Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Average does not calculate after For Loop

java loops for loop

Best Answer wim DC, 22 April 2015 - 11:04 PM

(int)((totalOccuRooms/totalRooms)*100);

Since all you have are of type int here, decimals can't be used. Everything that would go beyond the comma will simply be lost.

so if totalrooms is 100, totaloccurooms is 50 Java will do

(50 / 100)*100 = 0.5 * 100 = 0 * 100 = 0

 

The red is not stored anywhere, so just dropped.

You can either turn one of the numbers into a double temporarily

(int)(((double)totalOccuRooms/totalRooms)*100);

Or be smarter about the positioning and move the 100 to the front, so java won't need the decimal part.

average = 100 * totalOccuRooms / totalRooms;
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 dreddooo

dreddooo

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 22 April 2015 - 04:52 PM

Hi! I need help in understanding why the average does not calculate the data from my accumulator after the for loop ends.

 

We were asked for our homework to create a "hotel occupancy" program which asks the user to put in the number of floors, the number of rooms in a floor, and the number of rooms occupied.

 

LIMITATIONS:

  • 13th floor is skipped
  • Number of floors should NOT be less than 1
  • Number of rooms in a floor should NOT be less than 10
  • Occupied rooms should NOT be greater than number of rooms

PROBLEM:

The value for totalRooms and totalOccuRooms works but the average doesn't. Need help with this one.

 

MY CODE:

import java.util.Scanner;

public class Lab7a //Hotel Occupancy
{
   public static void main(String[]args)
   {
      Scanner keyboard = new Scanner(System.in);
      
      int numFloors; 
      int average;
      int floorTotal, totalRooms = 0;
      int floorOccu, totalOccuRooms = 0;
            
      do
      {
         System.out.println("How many floors are there in the hotel?");
         numFloors = keyboard.nextInt();
      }while (numFloors < 1);
      
      for (int counter = 1; counter <= numFloors; counter++)
      {
         if (counter == 13)
            continue;
         do 
         {
            System.out.println("\nHow many rooms are there in floor " + counter + "?");
            floorTotal = keyboard.nextInt();
         }while (floorTotal < 10);
         
         do
         {
            System.out.println("How many are occupied?");
            floorOccu = keyboard.nextInt();
         }while (floorOccu < 0 || floorOccu > floorTotal);
         
         totalRooms += floorTotal;
         totalOccuRooms += floorOccu;   
      }
        
      average = (int)((totalOccuRooms/totalRooms)*100);                 
      System.out.println("\nThe Hotel has " + totalRooms + " rooms in total.");
      System.out.println(totalOccuRooms + " of the rooms are occupied.");
      System.out.printf("%d of the rooms are occupied.", average);
      
   }
}

This will result to this:

  How many floors are there in the hotel?
  1

 

  How many rooms are there in floor 1?

  100
  How many are occupied?

  96
 

  The Hotel has 100 rooms in total.
  96 of the rooms are occupied.
  0 of the rooms are occupied.

 



#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts

Posted 22 April 2015 - 11:04 PM   Best Answer

(int)((totalOccuRooms/totalRooms)*100);

Since all you have are of type int here, decimals can't be used. Everything that would go beyond the comma will simply be lost.

so if totalrooms is 100, totaloccurooms is 50 Java will do

(50 / 100)*100 = 0.5 * 100 = 0 * 100 = 0

 

The red is not stored anywhere, so just dropped.

You can either turn one of the numbers into a double temporarily

(int)(((double)totalOccuRooms/totalRooms)*100);

Or be smarter about the positioning and move the 100 to the front, so java won't need the decimal part.

average = 100 * totalOccuRooms / totalRooms;


#3 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 23 April 2015 - 06:16 AM

It's the classic integer division problem.  I ask this type of question during EVERY interview.  Any programmer with any real experience has had this issue.  The good thing is that once you understand it, you'll never make that mistake again.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#4 charlesdavis

charlesdavis

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 24 April 2015 - 04:08 AM

I am agree with wim DC she is saying right, please try her code, definately it will work..


Edited by charlesdavis, 24 April 2015 - 04:09 AM.


#5 dreddooo

dreddooo

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 24 April 2015 - 09:28 AM

Thanks wim DC! I used your first solution and examined why it worked. I must've forgotten how java reads the codes and the .5 your example slipped past my mind. :thumbup1: 

 

lespauled, I will take note of that and master it. Thanks!






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download