jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes problem with variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with variable" Watch "problem with variable" New topic
Author

problem with variable

Dominic Griffin
Greenhorn

Joined: Jun 10, 2009
Posts: 16
I am trying to calculate occupancy the occupancy rate of an hotel. The expression is returning a zero. Does anyone see an obvious solution?


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

The variables on the right are ints, so when you do the division, it will truncate the non-integer portion. The variable that you are assigning to is also an int, and hence, can't hold the non-integer portion.

It may be a good idea to cast to floating point before doing the division -- and assign it to a floating point variable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
In other words, your program has only two possibilities. Hotel is full, or hotel is empty.
Dominic Griffin
Greenhorn

Joined: Jun 10, 2009
Posts: 16
The lightbulb just clicked. It all makes sense now. Thank you gentlemen!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with variable
 
Similar Threads
Input Validation Help
I keep having a issue with variables and methods.
Cannot throw exception
Help with assigning Array Size
try...catch blocks giving <identifier> expected error