2

I have the following code:

int aGrade=7;
int asteriskCount=((aGrade/100)*50);
System.out.print(asteriskCount);

for(int i=0; i<=asteriskCount; i++)
{
    System.out.print("*");
}

asteriskCount evaluates to zero, so the code only prints one asterisk instead of the four I expected. Why?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110

5 Answers5

4

It's due to integer division. By using a floating value for 100.0 instead of 100, the division is performed using floating point.

Try

int asteriskCount = (int)((aGrade/100.0)*50);
                                  ^^^^^
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
2

There are a few problems with your code:

Integer division:
Since you're using integers, here's what actually happens in the calculation:

(aGrade/100) -> 0
((aGrade/100) * 50) = (0 * 50) -> 0

How to fix:
You can either use floating point calculation (as suggested in other answers), or simply invert the order of operations:

int asteriskCount = ((aGrade * 50) / 100);

That will solve the problem, and will result in 3 instead of 0.

Lack of rounding:
As mentioned above, even if you correct your calculation, you'll still get the asterisk count of 3 and not 4. The reason is that you're not rounding the result.

How to fix:
You can achieve rounding in integer division by adding half of the amount your dividing by before the division takes place. Why does this work ? Since it simulates the way you round things normally, i.e. adding 0.5 after the division and trimming the remainder.
In this example the calculation should look like this:

int roundUp = (100 / 2);
int asteriskCount = ((aGrade * 50 + roundUp) / 100);

And there you have it, now you get 4 !

Incorrect iteration length:
There's yet another problem that will cause you to get the wrong number of asterisks printed, and that is that your iterations prints one asterisk too many. This is the reason that at the moment, you're seeing one asterisk instead of none (remember that the original calculation results in 0).
When you want to iterate a certain number of times, remember that if you start from 0, you should finish before the number is reached, otherwise, you're iterating once too many.

How to fix:
The iteration should be:

for (int i=0; i<asteriskCount; i++) {
    System.out.print("*");
}

Good luck !

ethanfar
  • 3,733
  • 24
  • 43
1

You may try this:

Double asteriskCount= Double((aGrade/100.0)*50);

The problem is integer/integer division is int. You need to cast it to Double and store it in a double.

But if you want to store it in an int then cast it in int.

int asteriskCount = (int)((aGrade/100.0)*50);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Yes it is due to integer division, like he said.

When you divide 5 by 2, the answer is 2.5. However if you use a integer to store the answer, it will appear as just 2.

Likewise, when you divide 7 by 100, your answer is 0.07, therefore the stored integer value is just 0.

Try this:

double aGrade = 7;
int asteriskCount = (int)((aGrade/100)*50);
Lee Jun Wei
  • 310
  • 3
  • 11
0

When you do a division like this aGrade/100 = 7/100 Java stores only the integer part of the result. i.e 7/100 = 0. So it doesn't matter what you multiply this result with afterwords, the answer will always be zero. This is called an integer division. If you want this calculation to return 0.07 you have to do divide with a double. i.e. : (aGrade/100.0)*50 and then when you multiply with 50, to use the result in your for loop, cast the second calculation to an int:

int asteriskCount = (int)((aGrade/100.0)*50);

gkrls
  • 2,618
  • 2
  • 15
  • 29