alan_ogz83
alan_ogz83

Reputation: 71

Java SE do while loop issues

I have a problem with my java application when i compile my codes it says that it cannot find symbol roomLength. what the application supposed to do is prompt the user to input a room name then if it is "quit" then it has to close the program. otherwise it will prompt for the length, width and height of the room. if any of these dimensions is zero or lesser, it will reprompt for the dimensions again.

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 here's my main method.

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();
    if(roomName.equals("quit"))
    {
        userInput.close();
    } else
    {
        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();

        Room r = new Room(roomName, roomLength, roomWidth, roomHeight);
        }
        while (roomLength > 0 || roomWidth > 0 || roomHeight > 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());

    }
}
}

Thanks in advance! ;)

There's another question almost similar to this question except it supposed to loop back to the first prompt allowing the user to input another room's name and its dimensions.

Java do while loop back to the beginning of application

Cheers!

Upvotes: 0

Views: 127

Answers (2)

shmosel
shmosel

Reputation: 50776

You're trying to access variables outside the scope they're declared in. You have to declare the variables outside the do so you can access them in the while:

    double roomLength;
    double roomWidth;
    double roomHeight;
    do
    {
    Scanner dimensionsInput = new Scanner(System.in);
    System.out.print("Please enter the dimensions of the room, length, width and height accordingly");

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

    Room r = new Room(roomName, roomLength, roomWidth, roomHeight);
    }
    while (roomLength > 0 || roomWidth > 0 || roomHeight > 0);

But I see now that Room is also declared with the wrong scope. You have to declare it before the loop if you want to access it afterwards. So a simpler solution might be:

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

Incidentally, it doesn't seem right that you're looping until all dimensions are 0. I suspect you mean to check == 0.

Upvotes: 2

Christoph Schubert
Christoph Schubert

Reputation: 1124

You have to declare the variables roomLength, roomWidth and roomHeight in front of the do-while loop.

Like this:

double roomLength = 0;
double roomWidth = 0;
double roomHeight = 0;
do { 
    dimensionsInput = new Scanner(System.in);
    System.out.print("Please enter the dimensions of the room, length, width and height accordingly");

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

    Room r = new Room(roomName, roomLength, roomWidth, roomHeight);
} while (roomLength > 0 || roomWidth > 0 || roomHeight > 0);

The problem is the scope of your variables. roomLength, roomWidth and roomHeight are only visible inside the do-block. But the statements inside the while are outside of the do-block. Thats why the variables could not be found.

Upvotes: 2

Related Questions