4

I need to ask the user to input a number to be used as the start of a range, and then input another number that is the end of the range. The start number has to be 0 or greater and the end number cannot be larger than 1000. Both numbers must be divisible by 10. I have found a way to meet these conditions, however if they are not met my program just tells the user that their input was incorrect. Is it possible for me to code it so that after the user inputs it will check to make sure the conditions are met, and if they are not loop back and make them input again. Here is the code I have so far.

    Scanner keyboard = new Scanner(System.in);
    int startr;
    int endr;
    System.out.println("Enter the Starting Number of the Range: ");
    startr=keyboard.nextInt();
    if(startr%10==0&&startr>=0){
        System.out.println("Enter the Ending Number of the Range: ");
        endr=keyboard.nextInt();
        if(endr%10==0&&endr<=1000){

        }else{
            System.out.println("Numbers is not divisible by 10");
        }
    }else{
        System.out.println("Numbers is not divisible by 10");
    }

3 Answers 3

7

Easy with do-while:

Scanner keyboard = new Scanner(System.in);
int startr, endr;
boolean good = false;
do
{
  System.out.println("Enter the Starting Number of the Range: ");
  startr = keyboard.nextInt();
  if(startr % 10 == 0 && startr >= 0)
    good = true;
  else
    System.out.println("Numbers is not divisible by 10");
}
while (!good);

good = false;
do
{
    System.out.println("Enter the Ending Number of the Range: ");
    endr = keyboard.nextInt();
    if(endr % 10 == 0 && endr <= 1000)
      good = true;
    else
      System.out.println("Numbers is not divisible by 10");
}
while (!good);

// do stuff
Sign up to request clarification or add additional context in comments.

3 Comments

Just a quick follow up question, the (!good) in the while statement is saying what exactly? while good = original value?
good means good == true and !good means good == false (but == boolean condition is generally bad practice).
This keeps throwing me an IllegalStateException at the nextInt() statement.
3

You need to use a while, something like:

while conditionsMet is false
    // gather input and verify
    if user input valid then
        conditionsMet = true;
end loop

should do it.

1 Comment

I would recommend making the code closer to Java - while (!conditionsMet), if (/*user input valid*/), no then and no end loop. { and } optional.
0

The all-purpose procedure is:

  1. Read the input in an infinite loop.
  2. Use a break; statement to exit the loop when the conditions are met.

Example:

Scanner keyboard = new Scanner(System.in);
int startr, endr;

for (;;) {
    System.out.println("Enter the starting number of the range: ");
    startr = keyboard.nextInt();
    if (startr >= 0 && startr % 10 == 0) break;
    System.out.println("Number must be >= 0 and divisible by 10.");
}

for (;;) {
    System.out.println("Enter the ending number of the range: ");
    endr = keyboard.nextInt();
    if (endr <= 1000 && endr % 10 == 0) break;
    System.out.println("Number must be <= 1000 and divisible by 10.");
}

If after invalid input you want to display just the error message without repeating the initial prompt message, move the initial prompt message just above/outside the loop.

If you do not have need for the separate error message, you can re-arrange the code to use a do-while loop to check the conditions, which is just a little shorter:

Scanner keyboard = new Scanner(System.in);
int startr, endr;

do {
    System.out.println("Enter the starting number of the range.");
    System.out.println("Number must be >= 0 and divisible by 10: ");
    startr = keyboard.nextInt();
} while (!(startr >= 0 && startr % 10 == 0));

do {
    System.out.println("Enter the ending number of the range.");
    System.out.println("Number must be <= 1000 and divisible by 10: ");
    endr = keyboard.nextInt();
} while (!(endr <= 1000 && endr % 10 == 0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.