mercredi 1 août 2018

How to re-run code after invalid scanner input in JAVA

I just started learning JAVA and I can't find a way to re-run this code after a user enters a wrong data type. I've searched on stack overflow but I couldn't find a solution. Thanks.

int j = 0;
System.out.println("Hello, how many numbers would you like to save?");

boolean hasNextInt2 = scanner.hasNextInt();

if(hasNextInt2){
    j = scanner.nextInt();
} else {
    System.out.println("Please enter a valid number");

}

Aucun commentaire:

Enregistrer un commentaire