Results 1 to 8 of 8
  1. #1
    dookie1293 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    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.

  5. #5
    dookie1293 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by hla40 View Post
    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

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

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Dookie

    Ignore that post. The three previous posts explain your problem.

  8. #8
    dookie1293 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Okay, then!

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

    I fixed it, and it worked.

Similar Threads

  1. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 10:15 AM
  2. Java Result: 255
    By MadJack in forum New To Java
    Replies: 14
    Last Post: 11-11-2010, 08:54 AM
  3. How to format the result
    By galagali in forum New To Java
    Replies: 3
    Last Post: 07-11-2010, 12:15 AM
  4. uncorrect result
    By jamborta in forum New To Java
    Replies: 3
    Last Post: 11-11-2009, 01:17 PM
  5. getting a random result
    By gradon in forum New To Java
    Replies: 2
    Last Post: 07-19-2007, 03:54 PM

Posting Permissions

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