Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
116 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...