0

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!

Community
  • 1
  • 1
alan_ogz83
  • 73
  • 1
  • 2
  • 10

2 Answers2

2

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.

Christoph Schubert
  • 1,089
  • 1
  • 8
  • 16
2

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.

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • thanks i manage to get it working now due to you guys help and explanation.i didn't know that you could instantiate an object in splitted forms. – alan_ogz83 Jun 29 '14 at 10:19
  • ok now i have another new question. how can loop back the application to the beginning? i mean right after getting all the intended inputs from the user. how can i loop back the application to where it prompts the user for another room attributes again? thanks! – alan_ogz83 Jun 29 '14 at 10:22
  • A short search here on SO resulted in: [link](http://stackoverflow.com/questions/21215914/how-to-repeat-something-until-correct-input-is-given-in-java/21215984#21215984) If you have a concrete question/problem its easier to create a new question. – Christoph Schubert Jun 29 '14 at 10:25
  • @alan_ogz83 Throw the whole thing in another loop: `String roomName; while(!(roomName = userInput.next()).equals("quit")) { /* inner loop */ } userInput.close();` – shmosel Jun 29 '14 at 10:31
  • @shmosel oic i tried although it kinda worked but it went to the second prompt instead of the first prompt asking for the room's name. – alan_ogz83 Jun 29 '14 at 11:14
  • @shmosel mate i have created another question so tat you guys can see what's the problem with the application. thanks! :) http://stackoverflow.com/questions/24475590/java-do-while-loop-back-to-the-beginning-of-application – alan_ogz83 Jun 29 '14 at 11:20