Question

I'm trying to get my Do..While loop to display an error message to why it is re-asking for the user input again. This is my code for it:

do {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();   
}

// Loop until user input is equal to or less than the maximum floors
while( current_Floor > MAX_FLOORS );{
    System.out.println("Please enter a floor which is less than 8");
}

The problem with this code is that the "Error" message which i've added appears after a correct user input has been entered, when I need it to display before the user input is added in. Any ideas?

Was it helpful?

Solution

Your formatting is all screwy, which is probably what's confusing you. The ; at the end of your while ends the do-while. The braces after that, containing the "error" statement, are just a static set of braces: they're not associated with your loop in any way.

If you format your code more legibly, it looks like this, which makes it clear that your "less than 8" message is placed incorrectly, and will always be printed, once you exit the loop.

do {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();   
} while( current_Floor > MAX_FLOORS );

{ // Useless braces!
    System.out.println("Please enter a floor which is less than 8");
} // Useless braces!

If you want to loop until some condition is met while informing the user of "failed" iterations, the usual idiom is generally to loop forever, and then break when your condition is met:

while (true) {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();   
    if (current_Floor <= MAX_FLOORS) {
        break;
    }

    // This prints only if the user entered a bad number.
    System.out.println("Please enter a floor which is less than 8");
}

Alternately, you can "extract" the first iteration of the loop, and then use a conventional while loop (or do-while if you're really committed to that structure for some reason) to ensure that the input is acceptable. Personally, I find the previous version a little cleaner, but that's really just down to personal preference and coding style.

// Initial input
System.out.println("\nPlease enter the floor you are on: ");
current_Floor = in.nextInt();

// Loop until the input is acceptable
while (current_Floor > MAX_FLOORS) {
    System.out.println("Please enter a floor which is less than 8");
    current_Floor = in.nextInt();   
}

As an aside, it's kind of nasty to be referring to MAX_FLOORS in the code, while hard-coding the magic number 8 into your error message: it would be better to use some string formatting to tell the user what the actual maximum floor number is.

System.out.println(String.format("Please enter a floor which is less than %d", MAX_FLOORS+1));

As another aside, your naming scheme is kind of weird: by convention, most variable names in Java should use camelCase, without underscores, e.g. currentFloor. Your MAX_FLOORS is okay though, since it seems to be a static/enum-like variable, which do conventionally use capslock with underscores.

OTHER TIPS

Java do while syntax is as follows:

do {
    //block of code to execute
} while([some boolean condition]);

Please look on Flow chart of a do-while loop:

enter image description here

And Read

That's not how do-while loops work in Java. I'd recommend sticking with a while loop since your initial pass is a little different from each subsequent one.

System.out.println("Please enter a floor which is less than 8");
current_Floor = in.nextInt();

while (current_Floor > MAX_FLOORS) {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();    
}

Here you prompt the user for an input. If it's invalid, you enter the loop where you display the error message and prompt them again. This will continue until the user behaves.

Your problem is that you are misusing the do-while loop.

do{
    ...STUFF...
}while( $CONDITION );
...MORE STUFF...

Works like this

  1. do STUFF
  2. calculate condition
  3. if CONDITION is true, go to 1, if not go to 4
  4. do MORE STUFF

In you case, STUFF is "ask the user for a floor", the condition is "is the floor the user selected greater than the max floor?", and MORE STUFF is "Ask the user to enter a floor less 8".

What you do is probably have the CONDITION be keep asking until a valid floor is selected, and have the check in an if statement inside the do-while loop.

The "do while" isn't the best type of loop to use here, because you need to check the condition in the middle of the repeated part, not at the end. It's possible to do this with "do while", but you end up checking the condition twice.

In order not to repeat code, there's a good alternative. The first line means "loop forever", but it will actually just loop until the "break" is encountered, which happens when the user enters correct input.

for(;;) {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();   
    if (current_Floor <= MAX_FLOORS) {
        break;
    }

    System.out.println("Please enter a floor which is less than 8");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top