I am developing an application where I pass 1 variables int through a string of an activity to next activity, in the next activity I take that string and return it again and an int, then I calculate a percentage and display and a textview, the past variable and approx1 so I check if it is not empty, then I calculate the percentage ex: ((3/45) * 100) and display in a text view, reviewing again for string ... but in any way I make a mistake, what can be ?
Bundle bundle = getIntent (). GetExtras ();
String aprox1 = bundle.getString ("aprox1");
if (aprox1! = null)
try {
num3 = Integer.parseInt (aprox1);
result = Math.round ((num3 / 45) * 100);
TextView counter2 = (TextView) findViewById(R.id.textView16);
String abcString2 = Integer.toString (result);
counter2.setText (abcString2);
}
catch (NumberFormatException e) {
}
You need to have {} after the if statement like so:
if (aprox1! = null) {
try {
num3 = Integer.parseInt (aprox1);
result = Math.round ((num3 / 45) * 100);
TextView counter2 = (TextView) findViewById(R.id.textView16);
String abcString2 = Integer.toString (result);
counter2.setText (abcString2);
}
catch (NumberFormatException e) {
}
}
Its also worth noting that because num3 is an integer when you divide it by 45 you will get an Integer not a percentage.
To fix this issue, make num3 a double or cast either num3 or 45 to a double before computing the division.
For example, a simple fix would include changing line 4 to the following:
result = Math.round ((num3 / 45.0) * 100);