Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Integer division round up

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.");
    }
}
like image 379
Sam Young Avatar asked Nov 28 '22 11:11

Sam Young


2 Answers

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

like image 98
surya Avatar answered Dec 05 '22 02:12

surya


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.

like image 29
Rahul Avatar answered Dec 05 '22 02:12

Rahul