Sarina Foreman
Sarina Foreman

Reputation: 11

Java do loop repeat until valid entry

I'm creating a Java project using a do-while loop to get a number from the user between 5 and 15. After this, I will create a square and triangle using the number the user entered.

I'm currently stuck on making my question repeat. After I run the program it runs fine until I input a number the second time the user is prompted to enter a number. It won't print if the number is invalid or not.

I tried moving the second "Enter a number..." inside of the do-loop but that just printed with the "Sorry, invalid" prompt. I did something similar with a case statement inside a while loop and it ran fine but I'm having difficulty with the do loop.

Can someone point me in the right direction?

import java.util.Scanner;
public class doLoop {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);

    int number;
    int minimum = 5;
    int maximum = 15;

    System.out.print("Enter a number between " + minimum + " and " + maximum + ":" );



        do {
            number = input.nextInt();
            if (number >= minimum && number <= 15) 
                break;
            else 
                System.out.print("Sorry, invalid");
            break;
        } while (false); 
        System.out.print("\nEnter a number between " + minimum + " and " + maximum + ":" );
        number =  input.nextInt();
    }
}

Upvotes: 0

Views: 18902

Answers (2)

Royce
Royce

Reputation: 189

There is a logical error in your code in the placement of your break and scanner input. If you really want to stay true to what you have, I find it much simpler to just add a boolean check instead of using a break. Below you can see I also put final so that way you know, and is a best practice that, that variable should not be altered or tampered with in code. I added a boolean since you wanted to check the do while for false, then we set it to true if it passes the if statement you made.

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int number;
boolean check = false;

// Final, because they are constant through-out the program
final int minimum = 5;
final int maximum = 15;

do {
    System.out.print("Enter a number between " + minimum + " and " + maximum + ":" );
    number = input.nextInt();
    if (number >= minimum && number <= maximum) 
        check = true;
    else 
        System.out.println("Sorry, invalid");
    break;
} while (check); 


}

Upvotes: 0

alseether
alseether

Reputation: 1993

In general, use break inside a while loop is considered a bad practice. You should have something like this

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int number;
    int minimum = 5;
    int maximum = 15;

    do{
        System.out.print("Enter a number between" + " " + minimum + " " + "and" + " " + maximum + ":" );
        number = input.nextInt();
        if (number < minimum || number > maximum) 
            System.out.print("Sorry, invalid");
    } while (number < minimum || number > maximum); 

}

Upvotes: 2

Related Questions