File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes if statement query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if statement query" Watch "if statement query" New topic

if statement query

prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
I have the folowwing part of the code. My problem is when i go to the if block,it should calculate fatCalPercent from calories and grams of fat (each gram of ffat is 9 calories),which are given as input in the textfield and display line 1,2 n 3 according to the condition that fatCalPercent <=0.30.
I get the output as 0 in line 1 and line 2 displayed only once and all the other time line 3 is displayed. Please help me out.

Daniel Rhoades
Ranch Hand

Joined: Jun 30, 2004
Posts: 186
The fatCalPercent variable is not declared in your inner class - can we assume it is in the outer class?

Drinking more tea is the key...
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

I echo Daniel's question, and also point out that I do not see you calculating the fatCalPercent anywhere prior to your if statement. Therefore it is likely being initialized to 0, so your if statement is always true.

You are then calculating the fatCalPercent in the if statement:
    fatCalPercent= ((gramsofFat * 9))/(numberofCalories);
It seems to me that that calculation should occur before your if statement.
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
The real issue is integer math.

Line 1 prints 0 because the result is less than 1 and the result is truncated.

(int * int )/ int

Try using a floating point value for the calories per gram fat conversion:

fatCalPercent= ((gramsofFat * 9.0))/(numberofCalories);
[ March 14, 2005: Message edited by: Carol Enderlin ]
I agree. Here's the link:
subject: if statement query
jQuery in Action, 3rd edition