Home > front end >  How to make the program ask the user for input until they give a valid response?
How to make the program ask the user for input until they give a valid response?

Time:02-07

could you please help me to make this program to get a correct number? The program should check the input type, it should be int. Also, the program should check that the value is from 1 to 10. If these conditions are true, the value should be assigned to correctNumber. So far I came up to this piece of code but I have difficulties in making it work properly.

System.out.println("Enter a number from 1 to 10:");
    Scanner scanner = new Scanner(System.in);
    while (!scanner.hasNextInt() || !isCorrectNumber(scanner.nextInt())) {
                System.out.println("Incorrect input!");
                scanner.next();
            }
            int correctNumber = scanner.nextInt();
    
        }
    private static boolean isCorrectNumber(int n) {
        return size >= 1 && size <= 10;
    }

CodePudding user response:

You are asking for input 2 times in your while condition Use a variable to save the input and then work with that

public static void main(String[] args) throws IOException {
    
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    
    String x = input.readLine();
    while (!isInt(x)) {
        x = input.readLine();
    }
    System.out.println(x);
}

I ask for input, i get a String, now i have to check if this string can be a number. For that i've created a boolean function isInt(String) Takes the String and returns if it can be a number.

public static boolean isInt(String str) {//function that checks if String can be parsed into an integer
    try {
        Integer.parseInt(str);
        return true;
    }catch (NumberFormatException e) {
        return false;
    }
}

I hope i've been helpfull

CodePudding user response:

At || !isCorrectNumber(scanner.nextInt()) you will consume provided int so at

int correctNumber = scanner.nextInt();

you will attempt to read another int.

To have have access to provided value in many places like:

  • validation section,
  • and section where you move on when input is valid.

you need to store it in variable. For instance

System.out.println("Enter a number from 1 to 10:");
Scanner scanner = new Scanner(System.in);

int number = -1; //variable which will store number provided by user 
                 //we need to initialize it with some value, -1 has no special meaning
while (!scanner.hasNextInt() || !isCorrectNumber(number = scanner.nextInt())) {
    //                                           ^^^^^^^^^
    //assigns user input to `number` and passes that value to method
    System.out.println("Incorrect input! Please write integer between 1 and 10.");
    scanner.next();
}
//here (after loop) we know that `number` variable holds correct value
            

private static boolean isCorrectNumber(int n) {
    return size >= 1 && size <= 10;
}
  •  Tags:  
  • Related