Question

In: Computer Science

I'm trying to get my occupancy rate to output as 2 decimal places with a %...

I'm trying to get my occupancy rate to output as 2 decimal places with a % sign. I know this is a string but I need it to print as 2 decimal places like 59.23% or 25.36%

I have system.out.printf("Occupancy rate: %2s", occupancy + "%"); but I'm still not getting 2 decimal places or the % sign. How can I fix this? I'm in beginning java so I can't use anything advanced.

public class ResortAccomidations {
public static void main(String[] args) {
int totalNumberOfOccupiedRooms = 0; // Total number of floors
int totalNumberOfRooms = 0; // Total number of rooms

Scanner in = new Scanner(System.in); // Scanner object for user input

System.out.print("How many floors does the resort have?: "); // Get number of floors in resort
int floor = in .nextInt(); // To store number of floors

while (floor < 1) // Validate input
{
System.out.print("INVALID. How many floors does the resort have?: "); // Correct floors
floor = in .nextInt(); // To store correct number of floors
}
for (int i = 0; i < floor; i++) // Initialize floor at 0, test that floor is <0, increase floor per user input
{
System.out.println("\nFloor " + (i + 1)); // Displays floor
System.out.print("How many rooms does floor " + (i + 1) + " have?: "); // Get how many rooms
int rooms = in .nextInt(); // Store number of rooms
while (rooms < 10) // While rooms greater than 10
{
System.out.print("INVALID. Enter 10 or more: "); // Validate input < 10
rooms = in .nextInt(); // Store correct number of rooms
}
totalNumberOfRooms = rooms + totalNumberOfRooms; // Add rooms to total number of rooms

System.out.print("How many occupied rooms does floor " + (i + 1) + " have?: "); // Get number of rooms occupied
int occupied = in .nextInt(); // Store number of rooms occupied
while (occupied > rooms) // While occupied do not exceed rooms
{
System.out.print("INVALID. How many rooms occupied?: "); //Validate occupied do not exceed rooms
occupied = in .nextInt(); // Store correct number of rooms occupied
}
totalNumberOfOccupiedRooms = totalNumberOfOccupiedRooms + occupied; // Add occupied to total number of occupied rooms   
}
int vacancies = totalNumberOfRooms - totalNumberOfOccupiedRooms; // Vacancies
double occupancy = ((double) totalNumberOfOccupiedRooms / (double) totalNumberOfRooms) * 100; // Occupancy rate


System.out.printf("\nNumber of rooms: %d\n", +totalNumberOfRooms);
System.out.printf("Occupied rooms: %d\n", +totalNumberOfOccupiedRooms);
System.out.printf("Vacant rooms: %d\n", +vacancies);
System.out.printf("Occupancy rate: %.2s", occupancy + "%");

}
}

This is my output

How many floors does the resort have?: 2

Floor 1
How many rooms does floor 1 have?: 16
How many occupied rooms does floor 1 have?: 5

Floor 2
How many rooms does floor 2 have?: 16
How many occupied rooms does floor 2 have?: 15

Number of rooms: 32
Occupied rooms: 20
Vacant rooms: 12
Occupancy rate: 62

I need 2 decimal places and the % sign but no luck. Could someone please tell me how to get the correct output?

Solutions

Expert Solution

Use this line==>

System.out.printf("Occupancy rate: %.2f%s", occupancy,"%" );

//Use this code

import java.util.Scanner;

public class ResortAccomidations {
    public static void main(String[] args) {
        int totalNumberOfOccupiedRooms = 0; // Total number of floors
        int totalNumberOfRooms = 0; // Total number of rooms

        Scanner in = new Scanner(System.in); // Scanner object for user input

        System.out.print("How many floors does the resort have?: "); // Get number of floors in resort
        int floor = in .nextInt(); // To store number of floors

        while (floor < 1) // Validate input
        {
            System.out.print("INVALID. How many floors does the resort have?: "); // Correct floors
            floor = in .nextInt(); // To store correct number of floors
        }
        for (int i = 0; i < floor; i++) // Initialize floor at 0, test that floor is <0, increase floor per user input
        {
            System.out.println("\nFloor " + (i + 1)); // Displays floor
            System.out.print("How many rooms does floor " + (i + 1) + " have?: "); // Get how many rooms
            int rooms = in .nextInt(); // Store number of rooms
            while (rooms < 10) // While rooms greater than 10
            {
                System.out.print("INVALID. Enter 10 or more: "); // Validate input < 10
                rooms = in .nextInt(); // Store correct number of rooms
            }
            totalNumberOfRooms = rooms + totalNumberOfRooms; // Add rooms to total number of rooms

            System.out.print("How many occupied rooms does floor " + (i + 1) + " have?: "); // Get number of rooms occupied
            int occupied = in .nextInt(); // Store number of rooms occupied
            while (occupied > rooms) // While occupied do not exceed rooms
            {
                System.out.print("INVALID. How many rooms occupied?: "); //Validate occupied do not exceed rooms
                occupied = in .nextInt(); // Store correct number of rooms occupied
            }
            totalNumberOfOccupiedRooms = totalNumberOfOccupiedRooms + occupied; // Add occupied to total number of occupied rooms
        }
        int vacancies = totalNumberOfRooms - totalNumberOfOccupiedRooms; // Vacancies
        double occupancy = ((double) totalNumberOfOccupiedRooms / (double) totalNumberOfRooms) * 100; // Occupancy rate


        System.out.printf("\nNumber of rooms: %d\n", +totalNumberOfRooms);
        System.out.printf("Occupied rooms: %d\n", +totalNumberOfOccupiedRooms);
        System.out.printf("Vacant rooms: %d\n", +vacancies);
        System.out.printf("Occupancy rate: %.2f%s", occupancy,"%" );

    }
}

//Output

//If you need any help regarding this solution ......... please leave a comment ....... thanks


Related Solutions

I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
I'm trying to make this C++ loan calculator but I can't get the program to output...
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision. This is what I have....
I'm trying to get motivated in learning the Atiyah-Singer index theorem. In most places I read...
I'm trying to get motivated in learning the Atiyah-Singer index theorem. In most places I read about it, e.g. wikipedia, it is mentioned that the theorem is important in theoretical physics. So my question is, what are some examples of these applications?
I need my code output values edited to be formatted to two decimal places (i believe...
I need my code output values edited to be formatted to two decimal places (i believe using setprecision), i will need you to edit my code toformat each menu option to 2 decimal places: provided is my code #include <iostream> #include <iomanip> using namespace std; int main() {    double radius;    double base;    double height;    double length;    double width;    double Area;    const double PI = 3.14159;    int choice;    // display menu   ...
I'm trying to get my code to loop back to let the user input multiple patients....
I'm trying to get my code to loop back to let the user input multiple patients. import java.util.Scanner; public class XYZ {    public static void main(String[] args) {           String response;        do{            //import scanner for input            Scanner input = new Scanner(System.in);               //prompt user input for last name            System.out.print("Enter last name ");            //output for last name            String...
Find the effective annual rate of interest (as a %, 2 decimal places) at which an...
Find the effective annual rate of interest (as a %, 2 decimal places) at which an amount of $2,000 today will accumulate to $4900 in 8 years. (Solve using excel =RATE function; Answer in percentage rounded to two decimals without the % sign e.g. 1.8891 is 1.89)
Q1: Find the effective annual rate of interest (as a %, 2 decimal places) at which...
Q1: Find the effective annual rate of interest (as a %, 2 decimal places) at which an amount of $2,000 today will accumulate to $6000 in 8 years. (Solve using excel =RATE function; Answer in percentage rounded to two decimals without the % sign e.g. 1.8891 is 1.89) Q2: Which of the following is CORRECT? When discounting an amount to be received in one years' time at a rate that is quoted as 12% compounding quarterly, we can: Select one:...
I'm trying to get an understanding of this code as it was done in class, can...
I'm trying to get an understanding of this code as it was done in class, can someone please explain step by step what is happening? namespace BMI { class Program { static void Main(string[] args) { double height, weight; Console.Write("Enter person's height in inches: "); bool value1 = double.TryParse(Console.ReadLine(), out height); Console.Write("Enter weight in pounds: "); bool value2 = double.TryParse(Console.ReadLine(), out weight); if (!value1 || !value2) { Console.WriteLine("Not valid input."); } else { if (height < 5 || height >...
Javascript array of objects: I'm trying to get the input into an array of objects, and...
Javascript array of objects: I'm trying to get the input into an array of objects, and then display the information in a table using a for loop, but I don't think my path is right <!DOCTYPE html> <head> <title>Form</title> <meta charset="utf-8" /> <style media="screen"> h1 { text-align: center; } div { background-color: #pink; border: 2px solid green; padding: 15px; margin: 65px; } </style> <script> var list = []; total = 0; text = ""; function Product(item, quantity, costs){ this.item =...
What is the firm's inventory turnover ratio, ROA (in %, round to 2 decimal places), ROE (in %, round to 2 decimal places), profit margin (in %, round to 2 decimal places), EPS($), P/E ratio, market-to-book ratio?
The balance sheet and income statement shown below are for Pettijohn Inc. Note that the firm has no amortization charges, it does not lease any assets, none of its debt must be retired during the next 5 years, and the notes payable will be rolled over. Balance Sheet (Millions of $) Assets 2018 Cash and securities $ 1,554.0 Accounts receivable 9,660.0 Inventories 13,440.0 Total current assets $24,654.0 Net plant and equipment 17,346.0 Total assets $42,000.0 Liabilities and Equity Accounts payable...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT