I need to output Number of rooms : , Occupied rooms:, Vacant rooms:, and Occupancy rate:, I've written the java program but I can not get the output I need. What am I doing wrong here? This is a java program and I'm not to use anything advanced, just beginner java programming. Any help would be appreciated
import java.util.Scanner; // Needed for the Scanner class
/**
* This program will read in the number of floors the resort
* contains and the number of rooms occupied.
* @author Jessica
* @version 10/25/19
*/
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?: "); // Get how many rooms
int rooms = in.nextInt(); // Store number of rooms
while(rooms > 10) // While rooms no greater than 10
{
System.out.print("INVALID. How many rooms?: "); // Validate input
> 10
rooms = in.nextInt(); // Store correct number of rooms
totalNumberOfRooms = totalNumberOfRooms + rooms; // Add rooms to
total number of rooms
}
System.out.print("How many rooms occupied?: "); // 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
vacancies = in.nextInt(); // Store vacancies
int occupancy = (totalNumberOfOccupiedRooms / totalNumberOfRooms) *
100; // Occupancy rate
occupancy = in.nextInt();
System.out.printf("Number of rooms: ", + totalNumberOfRooms);
}
}
Answer:
import java.util.Scanner; // Needed for the Scanner class
/**
* This program will read in the number of floors the resort
* contains and the number of rooms occupied.
* @author Jessica
* @version 10/25/19
*/
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?: "); // Get how many rooms
int rooms = in.nextInt(); // Store number of rooms
while(rooms > 10) // While rooms no greater than 10
{
System.out.print("INVALID. How many rooms?: "); // Validate input
> 10
rooms = in.nextInt(); // Store correct number of rooms
}
totalNumberOfRooms = totalNumberOfRooms + rooms; // Add rooms to total number of rooms
System.out.print("How many rooms occupied?: "); // 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
int occupancy = (totalNumberOfOccupiedRooms / totalNumberOfRooms) *
100; // Occupancy rate
System.out.printf("Number of rooms: %d, ", + totalNumberOfRooms);
System.out.printf("Occupied rooms: %d, ", +
totalNumberOfOccupiedRooms);
System.out.printf("Vacant rooms: %d, ", + vacancies);
System.out.printf("Occupancy rate: %d", + occupancy);
}
}
this is the correct code, to print the output, you do not need to use in.nextInt() as it is used for taking in put from console.
At that point, the program was getting stopped. Instead use, System.out.printf().
Also,
totalNumberOfRooms = totalNumberOfRooms + rooms;
This line should be outside while loop as if user enters the correct number of rooms in the first time only, then the rooms were not getting added to the total number of rooms variable.
Output:

Hope you like it!
Please provide comments if you have any doubts!
How many floors does the resort have? : 2, Floor 1 How many rooms?: 1 How many rooms occupied?: 0 Floor 2 How many rooms?: 4 How many rooms occupied?: 2 Number of rooms: 5, Occupied rooms: 2, Vacant rooms: 3, Occupancy rate: 0
I need to output Number of rooms : , Occupied rooms:, Vacant rooms:, and Occupancy rate:,...
Write a complete Java program with methods that prompt user for the number of floors, rooms, occupied rooms in a hotel. You must validate floors, rooms, occupied rooms. Compute vacant rooms, occupancy rate on each floor and display rooms, occupied rooms, vacant rooms and occupancy rate for each floor. Use the given method names. See validation rules below: 1. getFloors(). This method prompts user for number of floors in a hotel and returns floors to the caller. 2. testFloors(floors). Do...
In C++ Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A loop should then iterate once for each floor. In each iteration, the loop should ask the user for the number of rooms on the floor and how many of them are occupied. After all iterations, the program should display how many room the hotel has, how many of them are occupied, how...
# JAVA In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on. Fill it with the appropriate values (31, 28, 31, 30, ...).The program reads a month and year from the user.When the year is a leap year, change the entry for February to 29.Print out how many days the given month has.CODE:import java.util.Scanner;public class NumberOfDays{ public static void main(String[] args) { // Declare and initialize daysOfMonth ....
How would I edit this Java program to end if the user inputs 0 or less? public static void main(String[] args) { Scanner sc = new Scanner(System.in); // to read input String Message; int m=0; int i=1; System.out.print("Please enter the message you would like displayed: "); Message=sc.nextLine(); while(true) { System.out.print("How many times would you like your message displayed?"); m = sc.nextInt(); break; } do { System.out.println(Message); //When I enter 0 or a negative number it still runs at least once,...
Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...
Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard. Chapter 5 Exercise 37: Java Programming * * (Decimal to...
Please answer both questions. Thanks
Question 26 (1 point) What is the output generated by the code segment below, assuming that data is a Scanner object used to read the following text: As the world turns. data.useDelimiter(" "); int count = 0; while (data.hasNext()) { char input data.next().charAt(0); if (! Character.isLetter (input)) { count++; System.out.println (count); A/ Question 27 (1 point) Assuming that the user provides 114 as input, what is the output of the following code snippet? Please make...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
C Programming I have this cost estimation program, but I want to add to it more functions to be more accurate and more useful. I want to add to the program an array and a loop that can help me with the materials that can be use to the building, as ceramic, wood and concrete and extra functions. ########## #include <stdio.h> int main(void) { float wid,len,yrs; int metersq = 2000; int floors,rooms,win,doors,restrooms,umet; char floortype[20]; char ti[20]; int woodfloor = 25;...
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...