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 problem with variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with variable" Watch "problem with variable" New topic

problem with variable

Dominic Griffin

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

Joined: Sep 28, 2004
Posts: 20514

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.


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

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

Joined: Jun 10, 2009
Posts: 16
The lightbulb just clicked. It all makes sense now. Thank you gentlemen!
I agree. Here's the link:
subject: problem with variable
It's not a secret anymore!