user1901231
user1901231

Reputation: 49

Java: do/while loop repeating even though condition has been met

I'm trying a simple do while loop that is suppose to run if the input is less than 1, and greater than 1000. It should ask for the user to input a correct number, on loop otherwise. What it seems to be doing now is repeating the loop one additional time, asking for the correct input, and then display the ending message. Unsure why it's repeating it, if the condition is met

String name = JOptionPane.showInputDialog(null,
        "Please enter students lastname");

int input = Integer.parseInt(JOptionPane.showInputDialog(null,
        "Please enter students ID"));

do {
    JOptionPane.showMessageDialog(null,
            "Please enter a student ID within the correct parameters");
    input = Integer.parseInt(JOptionPane.showInputDialog(null,
            "Please enter students ID"));
} while (input < 1 && input > 1000);

// Output dialog with user input
JOptionPane.showMessageDialog(null, "StudentID: " + input
        + "\nStudent Last: " + name);

Upvotes: 0

Views: 2340

Answers (3)

Barranka
Barranka

Reputation: 21047

I would change it with a while:

int input = Integer.parseInt(JOptionPane.showInputDialog(null,
    "Please enter students ID"));
while(input < 1 || input > 1000) {
    // Your stuff
}

The explanation

What I think was wrong is that, in the first place, there's no way any number can be (at the same time) less than 1 and more than 1000, so obviously, the valid input should be outside the specified range (that is, from -Infinity to 0 OR from 1001 to Infinity).

Secondly, what was mentioned in another answer: The do...while loop always runs at least once, and it repeats as long as the while condition is true. Since the input is being read before entering the loop, the 'confirmationalways takes place... What's the need to request a correction on a possibly correctinput` value?

What I think was wrong is a misunderstanding on the sense of the validation rule:

I'm trying a simple do while loop that is suppose to run if the input is less than 1, and greater than 1000

What does the and word means? I think it means that the input must be outside the given range.

Upvotes: -2

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58271

I think you while CONDITION is not correct as I read comments in print statement I believe you need

 while (input > 1 && input < 1000);

Because ID must not negative number.

Remember this condition is true if ID value is between 2 to 999.

As you commented: Just to clarify, if the user inputs a number outside of the range (1-1000),i.e. 2005, I want the loop to cycle, asking the user to input a number within the range,until that condition is met

do like, read comments to understand what My code is:

input = -1;
while(input < 1 || input > 1000){ 
//    ^              ^ OR greater then 1000
// either small then 1   
}

notice: I have punted OR instead of AND because either one condition fail you loop should be continue.

Upvotes: 1

Andy Thomas
Andy Thomas

Reputation: 86419

You're presenting the dialog at least twice -- once before the loop, once within the loop.

The do-while does not test the condition until after the loop has executed at least once.

You could either:

  • Eliminate the first call to show the input dialog.
  • Or change your do-while loop to a while loop.

In addition, see @GrailsGuy's comments on the loop test. Your current test will always fail.

Upvotes: 5

Related Questions