In: Computer Science
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?
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