# Keep getting result of 0.

• 06-09-2011, 04:39 AM
dookie1293
Keep getting result of 0.
I know I had to set the variables "occupied" and "total" to 0 to avoid getting an error, but whenever I run it, I get the "rate" keep equaling to 0. I need the rate to be the number of occupied rooms divided by the number of total rooms.

Code:

``` import java.util.Scanner; //gets the Scanner public class LBHotelOccupancy { //begin class         public static void main(String[] args)         { //begin main method                 int floor, floors; //The floor number and the total number of floors, respectively.                 Scanner input = new Scanner(System.in);                 //Politeness and and intro to the program                 System.out.println("Hello! I can calculate the occupancy rate for each floor in your hotel.");                 System.out.println("Please answer the following questions.");                        //Get the number of floors                 System.out.print("How many floors are in your hotel? ");                 floors = input.nextInt();                                 for(floor = 1; floor <= floors; floor++) //will increment the floor number with each loop until the total floors is reached                 {                 System.out.print("How many rooms are on floor " + floor + "? ");                 int total = input.nextInt(); //get total rooms on floor                 System.out.print("How many rooms on floor " + floor + " are occupied? ");                 int occupied = input.nextInt(); //get occupied rooms on floor                 System.out.println();                 double rate = occupied / total; //calculation for occupancy rate                 System.out.println("The occupancy rate for floor " + floor + " is " + rate + "."); //display occupancy rate                 System.out.println();                 }                         } //end main method } //end class```
• 06-09-2011, 04:47 AM
Junky
occupied and total are ints therefore integer division is applied. 5 / 9 = 0 not 0.5555555555555
• 06-09-2011, 04:48 AM
Fubarable
You're doing int division since total and occupied are both ints, and the result of an int division is always an int. So if you have 5/6 or 2/9 you'll get 0. The solution is to cast either the numerator or denominator as doubles in your division.
• 06-09-2011, 04:49 AM
Vase
Division of two ints will always result in an int; in this case, variable 'total' is always greater than or equal to variable 'occupied', so rate will always be zero.
• 06-09-2011, 04:58 AM
dookie1293
Quote:

Originally Posted by hla40
I was just browsing the forums and came across your post. One thing I noticed was you have: for(floor = 1; floor <= floors; floor++). When I run that code no matter what floor I put in it always comes back as "1". Try doing this: for(int i = 0; i <= floors; i++). And Changing all your "floor" to "floors" expect for where it is written text.

What do you mean it always comes back as one? It first asks how many total floors there are, and then it starts from the first floor asking for the information and keeps increasing until it reaches the last floor.
• 06-09-2011, 04:59 AM
Junky
What difference will that make. Giving a variable a different name will not change the behaviour of the code.
• 06-09-2011, 05:00 AM
Junky
Dookie

Ignore that post. The three previous posts explain your problem.
• 06-09-2011, 05:01 AM
dookie1293
Okay, then!

Thank you so much, everyone!!!! :)

I fixed it, and it worked. :(party):