Question

I'm trying to write a program that prompts the user to enter the total amount of floors in a hotel, the number of rooms in each floor, and the number of occupied rooms. In the end it should display the total # of rooms, the total # of rooms occupied, and the percentage of the rooms occupied. I am having problems with displaying the percentage of rooms occupied. I'm using all int numbers.

This is the equation that I put:

roomsOccPercentage = (totalRoomsOccupied * 100) / totalRooms ;

When I submit the program into my professor's Java runner it displays:

65 % of the Rooms are occupied.

But the one my professor provided outputted the answer 66 % instead so the program won't accept my file.

Does anyone know what I'm doing wrong? Is it a DecimalFormat error?

Edit: Here is the whole code

import java.util.Scanner; 
import java.text.DecimalFormat;

public class hw7_1 {
    public static void main(String[]args) {

        Scanner keyboard = new Scanner(System.in);
        DecimalFormat formatter = new DecimalFormat("#0");
        int totalFloors;
        int totalRooms = 0;
        int numFloors;
        int numRooms;
        int roomsOccupied;
        int totalRoomsOccupied = 0;
        int roomsOccPercentage = 0;

        //prompting users to input # of floors, no inputs below 1 floor
        do {
            System.out.println("Please enter the number of floors in the hotel: ");
            numFloors = keyboard.nextInt();

            if (numFloors < 1) {
                System.out.println("You have entered an invalid number of floors. ");
            }
        }
        while (numFloors < 1);

        //for loops on how many rooms on each hotel floors
        for ( int Floors = 1; Floors <= numFloors; Floors++) {
            if (Floors == 13 ) {
                continue;
            }

            do {
                System.out.println("Please enter the number of rooms on floor #: " + Floors );
                numRooms = keyboard.nextInt();

                if (numRooms < 10) {
                    System.out.println("You have entered an invalid number of rooms. ");
                }
            } while (numRooms < 10);

            System.out.println("Please enter the number of occupied rooms on floor #: " + Floors);
            roomsOccupied = keyboard.nextInt();

            totalRooms = totalRooms + numRooms;
            totalRoomsOccupied = totalRoomsOccupied + roomsOccupied;
            roomsOccPercentage = (totalRoomsOccupied * 100) / totalRooms ;
        }
        System.out.println("\nThe hotel has a total of " + totalRooms + " rooms.");
        System.out.println(totalRoomsOccupied + " of the rooms are occupied.");
        System.out.println(formatter.format(roomsOccPercentage) + "% of the rooms are occupied.");
    }
}
Was it helpful?

Solution

You need to make your roomsOccPercentage a double first.

double roomsOccPercentage = 0.0;

and then cast either of the operands so avoid an integer division.

roomsOccPercentage = (totalRoomsOccupied * 100.0) / totalRooms;

You can either use an explicit cast like (double)totalRoomsOccupied or just make 100 as 100.0 which is a floating point number, thus making the division too, a floating point one and not an integer division.

OTHER TIPS

Note that java plays with its own rule, You need to obey them. Decide if you want floor or ceil of the result, Follow example given below.

double roomsOccPercentage;
roomsOccPercentage =Math.ceil((double)5/4); //will be 2
roomsOccPercentage =Math.ceil(5/4); //will be 1
roomsOccPercentage =Math.floor((double)5/4); //will be 1

I hope you can now find java intresting

a / b + a % b / (b/2) ??
1 divide by 3 should be 0
1 / 3 + 1 % 3 / (3/2) = 0 + 1 / 1 = 1 which is NOT 0

I recommend this:

(a + a % b) / b

There is nothing called rounding in integer dividing since it will take integer part only in divide result. use double value for your calculations and use Math.round().

Heres a simple way to round up:

double dnum; = 47.213; //make this your value to round up
int inum; //null for now, will be your rounded up integer
if(dnum > (int)num)
     inum = (int)(num + 1.0);
else
     inum = (int)num;

If its not already an integer, it adds one and casts it down. If it is an integer, it casts it as an integer.

Given a/b, if you want it to round to the nearest integer, you need to add 1 if the remainder is half or more than half of b, therefore:

a / b + a % b / (b/2)

Will do the trick.

If you prefer to go into doubles to make an integer division that rounds up, you can do that. If you prefer not to, you don’t need to, it’s just a very slight bit tricky:

private static int divideRoundUp(int denominator, int divisor) {
    return (denominator + divisor - 1) / divisor;
}

Let’s try it:

    System.out.println(divideRoundUp(196, 3));
    System.out.println(divideRoundUp(197, 3));
    System.out.println(divideRoundUp(118, 2));
    System.out.println(divideRoundUp(119, 2));

This prints:

66
66
59
60

If there’s no remainder, as in 118 / 2, you get the normal result. In other cases the result is rounded up. How to handle negative denominator and/or divisor is left as an exercise for the reader, though.

In school many of us learned that 65.5 and higher should be rounded up to 66, where as numbers between 65.0 and 65.5 should be rounded down to 65. If you want this, the method is a bit different:

private static int divideRoundHalfUp(int denominator, int divisor) {
    return (denominator + divisor / 2) / divisor;
}

Demonstration:

    System.out.println(divideRoundHalfUp(196, 3));
    System.out.println(divideRoundHalfUp(197, 3));
    System.out.println(divideRoundHalfUp(118, 2));
    System.out.println(divideRoundHalfUp(119, 2));

Output:

65
66
59
60
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top