While, Do While, and For Loops

I am having a problem with coming up with the kind of loops I need in

Page 1 of 1

8 Replies - 1090 Views - Last Post: 21 October 2009 - 11:01 AM Rate Topic: -----

#1 cmole001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 20-October 09

While, Do While, and For Loops

Post icon  Posted 20 October 2009 - 05:11 PM

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

public class HotelOccupancy
{
public static void main(String[] args)
{
final int MIN_FLOORS = 1;
final int MIN_ROOMS = 10;
int inputFloors;
int roomCounter;

Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0.00%");

System.out.print("Enter the number of floors: ");
inputFloors = keyboard.nextInt();

while (inputFloors < MIN_FLOORS)
{
System.out.println("Invalid amount of floors.");
System.out.print("Please enter 1 or more: ");
inputFloors = keyboard.nextInt();
}

This program is supposed to calculate the occupancy rate for each floor of a hotel. The program starts by asking the user for the number of floors in the hotel. The number is supposed to be at least one. The program is then supposed to ask the user for the number of rooms on each floor (has to be at least 10 per floor) and the amount occupied (which of course cannot be greater than the number of rooms on the floor. After all that, the program is supposed to display the number of rooms the hotel has, the number of them that are occupied, the number of them that are vacant, and the occupancy rate for the hotel. The part that I am stuck on is the creation of the Loop that is supposed to get this user input. The last part seems simple enough to me, I just don't know whether or not to use a Do While Loop, or a For Loop to get that information. I don't know if I need a nested Loop or not. If anyone has any suggestions on how I can get this program to work, please reply back to this post. I don't want you to do my program for me, I just want to know what I can do to get this program working.

Is This A Good Question/Topic? 0
  • +

Replies To: While, Do While, and For Loops

#2 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: While, Do While, and For Loops

Posted 20 October 2009 - 05:21 PM

View Postcmole001, on 20 Oct, 2009 - 04:11 PM, said:

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

public class HotelOccupancy
{
public static void main(String[] args)
{
final int MIN_FLOORS = 1;
final int MIN_ROOMS = 10;
int inputFloors;
int roomCounter;

Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0.00%");

System.out.print("Enter the number of floors: ");
inputFloors = keyboard.nextInt();

while (inputFloors < MIN_FLOORS)
{
System.out.println("Invalid amount of floors.");
System.out.print("Please enter 1 or more: ");
inputFloors = keyboard.nextInt();
}

This program is supposed to calculate the occupancy rate for each floor of a hotel. The program starts by asking the user for the number of floors in the hotel. The number is supposed to be at least one. The program is then supposed to ask the user for the number of rooms on each floor (has to be at least 10 per floor) and the amount occupied (which of course cannot be greater than the number of rooms on the floor. After all that, the program is supposed to display the number of rooms the hotel has, the number of them that are occupied, the number of them that are vacant, and the occupancy rate for the hotel. The part that I am stuck on is the creation of the Loop that is supposed to get this user input. The last part seems simple enough to me, I just don't know whether or not to use a Do While Loop, or a For Loop to get that information. I don't know if I need a nested Loop or not. If anyone has any suggestions on how I can get this program to work, please reply back to this post. I don't want you to do my program for me, I just want to know what I can do to get this program working.


I don't understand why you need a loop to get user input.... You have the questions asked already, use the answers to those questions to reference your loops....Unless I am not understanding something...
Was This Post Helpful? 0
  • +
  • -

#3 cmole001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 20-October 09

Re: While, Do While, and For Loops

Posted 20 October 2009 - 05:30 PM

I thank you for responding to me :) . the program is supposed to keep going and ask the user for x amount of floors. Then the program is supposed to repeat itself x amount of times based on the amount of floors the user put in. When it repeats itself, it should ask each time "How many rooms are on floor x", then ask "how many are occupied". The program has to keep track of how many rooms the user put in total. Then spit it back out at the end. What I am confused on is what loop I should use to get this, and how I can make it free from errors at the same time.

This post has been edited by cmole001: 20 October 2009 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: While, Do While, and For Loops

Posted 20 October 2009 - 05:33 PM

hi
to get number of rooms for each floor
you can use a for loop in that way:
//Global variable:
int numberOfRooms = 0;
for(int i=1;i<=inputFloors;i++){
System.out.println("enter number of rooms in floor " +i);
numberOfRooms+= keyboard.nextInt();
//then also check input, in that case number of rooms is not less than 10
} 
System.out.println("numer of rooms in the hotel is " +numberOfRooms);



hope it helps :)
Was This Post Helpful? 1
  • +
  • -

#5 cmole001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 20-October 09

Re: While, Do While, and For Loops

Posted 20 October 2009 - 05:40 PM

That is sort of what I thought of doing, but how would I validate that when it asks for the number of rooms that are occupied afterwards?, as well as how to make the user not be able to enter in less than ten rooms per floor. The more I think of how deep and complex it is, the more it confuses me. I thank you for responding to my question and for your assistance.
Was This Post Helpful? 0
  • +
  • -

#6 cmole001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 20-October 09

Re: While, Do While, and For Loops

Posted 20 October 2009 - 06:14 PM

Okay, so far I have the following:

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

public class HotelOccupancy
{
	public static void main(String[] args)
	{
		final int MIN_FLOORS = 1;
		final int MIN_ROOMS = 10;
		int inputFloors;
		int numberOfRooms = 0;

		Scanner keyboard = new Scanner(System.in);
		DecimalFormat formatter = new DecimalFormat("#0.00%");

		System.out.print("Enter the number of floors: ");
		inputFloors = keyboard.nextInt();

		while (inputFloors < MIN_FLOORS)
		{
			System.out.println("Invalid amount of floors.");
			System.out.print("Please enter 1 or more: ");
			inputFloors = keyboard.nextInt();
		}

		for (int i = 1; i <= inputFloors; i++)
		{
			System.out.print("Enter number of rooms in floor " + i + ": ");
			numberOfRooms += keyboard.nextInt();
		}

		System.out.println("The number of rooms in the hotel is " +
							numberOfRooms);
	}
}



It works so far, but I don't know how to make it verify if the user puts in a number less than ten for the rooms and kick them back to the question. I also can't figure out how to make it ask for the number of rooms that are occupied on each floor. Does anyone have any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 cmole001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 20-October 09

Re: While, Do While, and For Loops

Posted 20 October 2009 - 08:00 PM

It would be greatly appreciated if someone can give me an assist with this, I can't get through the logic necessary to make Java work the way I want it to work for this program.
Was This Post Helpful? 0
  • +
  • -

#8 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: While, Do While, and For Loops

Posted 21 October 2009 - 01:24 AM

Try this out...hope it works.
public static void main(String[] args)
{
	final int MIN_FLOORS = 1;
	final int MIN_ROOMS = 10;
	int inputFloors;
	int numberOfRooms = 0;
	int inputRooms = 0;
	
	Scanner keyboard = new Scanner(System.in);
	DecimalFormat formatter = new DecimalFormat("#0.00%");

	System.out.print("Enter the number of floors: ");
	inputFloors = keyboard.nextInt();

	while (inputFloors < MIN_FLOORS)
	{
		System.out.println("Invalid amount of floors.");
		System.out.print("Please enter 1 or more: ");
		inputFloors = keyboard.nextInt();
	}

	for (int i = 1; i <= inputFloors;)
	{
		
		System.out.print("Enter number of rooms in floor " + i + ": ");
		inputRooms = keyboard.nextInt();
		if(inputRooms < MIN_ROOMS){
			System.out.println("Invalid number of rooms in floor! Please enter more than 10 rooms!");
			continue;
		}
		else { 
			numberOfRooms += inputRooms;
			i++;
		}
	}

	System.out.println("The number of rooms in the hotel is " +
						numberOfRooms);
}


Was This Post Helpful? 0
  • +
  • -

#9 cmole001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 20-October 09

Re: While, Do While, and For Loops

Posted 21 October 2009 - 11:01 AM

No, that didn't help me at all, but I think I have it now.

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

public class HotelOccupancy
{
	public static void main(String[] args)
	{
		int inputFloors;
		int numberOfRooms = 0;
		int totalNumberOfRooms = 0;
		int roomsOccupied = 0;
		int totalRoomsOccupied = 0;
		int totalRoomsVacant = 0;
		double occupancyRate = 0.0;
		
		Scanner keyboard = new Scanner(System.in);
		DecimalFormat formatter = new DecimalFormat("#0.00%");

		System.out.print("Enter the number of floors: ");
		inputFloors = keyboard.nextInt();

		while (inputFloors < 1)
		{
			System.out.println("Invalid amount of floors.");
			System.out.print("Please enter 1 or more: ");
			inputFloors = keyboard.nextInt();
		}

		for (int i = 1; i <= inputFloors; i++)
		{
			System.out.print("Enter number of rooms in floor " + i + ": ");
			numberOfRooms = keyboard.nextInt();			

			while (numberOfRooms < 10)
			{
				System.out.println("Invalid amount of rooms.");
				System.out.print("Please enter 10 or more: ");
				numberOfRooms = keyboard.nextInt();
			}
			totalNumberOfRooms += numberOfRooms;

			System.out.print("Enter number of occupied rooms on floor " + i + ": ");
			roomsOccupied = keyboard.nextInt();

			while (roomsOccupied > numberOfRooms || roomsOccupied < 0)
			{
				System.out.println("Invalid amount of rooms.");
				System.out.print("Please enter " + numberOfRooms + " or less: ");
				roomsOccupied = keyboard.nextInt();
			}
			totalRoomsOccupied += roomsOccupied;

		}
		totalRoomsVacant = totalNumberOfRooms - totalRoomsOccupied;
		occupancyRate = (double)totalRoomsOccupied / (double)totalNumberOfRooms;
		System.out.println("The number of rooms in the hotel is " + totalNumberOfRooms);
		System.out.println("The number of rooms occupied in the hotel is " +  totalRoomsOccupied);
		System.out.println("The number of rooms vacant in the hotel is " + totalRoomsVacant);
		System.out.print("The occupancy rate for the hotel is ");
		System.out.println(formatter.format(occupancyRate));
	}
}



I don't know what else I can do to refine it.
Was This Post Helpful? 1

Page 1 of 1