Thread: Keep getting result of 0.

1. Member Join Date
Jun 2011
Posts
7
Rep Power
0 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.

Java 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.");

//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  Reply With Quote

2. occupied and total are ints therefore integer division is applied. 5 / 9 = 0 not 0.5555555555555  Reply With Quote

3. 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.  Reply With Quote

4. Member Join Date
Jan 2011
Location
Gainesville, FL
Posts
45
Rep Power
0 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.  Reply With Quote

5. Member Join Date
Jun 2011
Posts
7
Rep Power
0  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.  Reply With Quote

6. What difference will that make. Giving a variable a different name will not change the behaviour of the code.  Reply With Quote

7. Dookie

Ignore that post. The three previous posts explain your problem.  Reply With Quote

8. Member Join Date
Jun 2011
Posts
7
Rep Power
0 Okay, then!

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

I fixed it, and it worked.   Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•