-1

This question is a follow-up from this link Java SE do while loop issues

This is an application asking for a room's name then second prompt to ask for its dimensions to perform some calculations. right after everything has been done, the application should be able to return back to the first prompt asking for the name of room etc.

class Room
{
private String name;
private double length;
private double width;
private double height;

public Room(String r, double l, double w, double h)
{
    name = r;
    length = l;
    width = w;
    height = h;
}


public String getName()
{
    return name;
}

public double getLength()
{
    return length;
}

public double getWidth()
{
    return width;
}

public double getHeight()
{
    return height;
}

public double getArea()
{
    double area = length*width;
    return area;
}

public double getVolume()
{
    double volume = length*width*height;
    return volume;  
}

public void setName(String s)
{
    name = s;
}
}

And my main method running the application.

import java.util.Scanner;

class TestRoom
{
public static void main(String [] args)
{
    Scanner userInput = new Scanner(System.in);
    System.out.println("Please enter the name of room");

    String roomName = userInput.next();
    while(!(roomName.equals("quit")))
    {           
        Room r;
        do
        {
            Scanner dimensionsInput = new Scanner(System.in);
            System.out.print("Please enter the dimensions of the room, length, width and height accordingly");

            double roomLength = dimensionsInput.nextDouble();
            double roomWidth = dimensionsInput.nextDouble();
            double roomHeight = dimensionsInput.nextDouble();

            r = new Room(roomName, roomLength, roomWidth, roomHeight);
        }
        while (r.getLength() == 0 || r.getWidth() == 0 || r.getHeight() == 0);


        System.out.println("The name of room: " + r.getName() + " Dimensions: L= " + r.getLength() + 
                                " X " + "W= " + r.getWidth() + " X " + "H= " + r.getHeight());
        System.out.println("Area of room= " + r.getArea());
        System.out.println("Volume of room= " + r.getVolume());

    }
    userInput.close();
}
}

Right now the application only returns to the second prompt instead of the first one so could you guys help me on this? Thanks in advance again guys!

Community
  • 1
  • 1
alan_ogz83
  • 73
  • 1
  • 2
  • 10
  • 1
    Isn't it obvious that you will need enclose the first prompt in the while loop? – Hovercraft Full Of Eels Jun 29 '14 at 11:20
  • The prompt for the name of the room should be inside the loop, and not outside, since you want it repeated. – JB Nizet Jun 29 '14 at 11:20
  • @JBNizet I have cut the prompt and paste it right above the code Room r; but although compilation is ok but it doesn't run. – alan_ogz83 Jun 29 '14 at 11:27
  • @HovercraftFullOfEels hi I have just done tat but compilation is ok but it doesn't run at all lol. sorry guys i'm very noob in programming please try to understand. :) – alan_ogz83 Jun 29 '14 at 11:28
  • 1
    Edit your question and post your new code at the bottom. Define "it doesn't run". What happens? You realize you must continue to read what the user entered *after* having asked him to enter something, right? – JB Nizet Jun 29 '14 at 11:30
  • 1
    Edit your question, show your latest attempt as additional code at the end of your original question, show errors that you're having with it. Also either user a debugger or use println statements to help you debug. – Hovercraft Full Of Eels Jun 29 '14 at 11:30
  • @HovercraftFullOfEels oic ok sure so sorry i seriously din know about the etiquettes but now i know so in future i will do as advised. thanks so much for your help anyway. – alan_ogz83 Jun 29 '14 at 11:39
  • @JBNizet mate thanks for telling me about the etiquettes i now know what to do right after each help i got from everyone. cheers! – alan_ogz83 Jun 29 '14 at 11:40

2 Answers2

1

You don't need to declare two scanners. You obviously had to include the first prompt into the outer while loop as well.

"Fixed" code:

import java.util.Scanner;

class TestRoom
{
    public static void main(String[] args)
    {
        Scanner userInput = new Scanner(System.in);
        String roomName = "";

        while (!(roomName.equals("quit")))
        {
            System.out.println("Please enter the name of room");

            roomName = userInput.next();

            Room r;

            do
            {
                System.out
                        .print("Please enter the dimensions of the room, length, width and height accordingly");

                double roomLength = userInput.nextDouble();
                double roomWidth = userInput.nextDouble();
                double roomHeight = userInput.nextDouble();

                r = new Room(roomName, roomLength, roomWidth, roomHeight);
            } while (r.getLength() == 0 || r.getWidth() == 0
                    || r.getHeight() == 0);

            System.out.println("The name of room: " + r.getName()
                    + " Dimensions: L= " + r.getLength() + " X " + "W= "
                    + r.getWidth() + " X " + "H= " + r.getHeight());
            System.out.println("Area of room= " + r.getArea());
            System.out.println("Volume of room= " + r.getVolume());
        }

        userInput.close();
    }
}
Bully WiiPlaza
  • 462
  • 1
  • 6
  • 16
  • wow thanks but what does this line mean? String roomName = ""; – alan_ogz83 Jun 29 '14 at 11:38
  • 1
    It means that the String is assigned an empty String. `null` is not good since calling a method on it would throw a `NullPointerException`. In this example, any String could be assigned except for `quit` since it would then skip the outer while loop. – Bully WiiPlaza Jun 29 '14 at 11:44
1

Try this:

 class TestRoom {

    static Scanner userInput = new Scanner(System.in);

    static Scanner dimensionsInput = new Scanner(System.in);

    public static void main(String[] args) {

        while(true) {
            prompt();
        }

    }

    private static void prompt() {
        System.out.println("Please enter the name of room");

        String roomName = userInput.next();

        if (roomName.equals("quit")){
            userInput.close();
            dimensionsInput.close();
            System.exit(0);
        }
        Room r;
        do
        {
            System.out
                    .print("Please enter the dimensions of the room, length, width and height accordingly");

            double roomLength = dimensionsInput.nextDouble();
            double roomWidth = dimensionsInput.nextDouble();
            double roomHeight = dimensionsInput.nextDouble();
            r = new Room(roomName, roomLength, roomWidth, roomHeight);
        } while (r.getLength() == 0 || r.getWidth() == 0
                || r.getHeight() == 0);


        System.out.println("The name of room: " + r.getName()
                + " Dimensions: L= " + r.getLength() + " X " + "W= "
                + r.getWidth() + " X " + "H= " + r.getHeight());
        System.out.println("Area of room= " + r.getArea());
        System.out.println("Volume of room= " + r.getVolume());
    }
}
rob
  • 1,286
  • 11
  • 12