This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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 ]
Consider Paul's rocket mass heater.
subject: if statement query
Similar Threads
Wrapper Class.
Sorting numbers using Compare method
Compiler error in JavaBean
Need suggestion on implementation