So I started learning java yesterday and I was trying to get the user to enter "5" and keep repeating until he/she got it. To prevent the program from crashing when you input a string I made the if/else statement. Everything works fine but when a person enters a int first then a string after it repeats the prompt "Enter the number 5 : " twice ! What am I doing wrong??
import java.util.Scanner;
public class chakad {
public static void main(String[] args) {
int number = 1;
Scanner input = new Scanner(System.in);
while (number != 5) {
System.out.println("Enter the number 5: ");
if (input.hasNextInt()) {
number = input.nextInt();
} else {
System.out.println("woops");
input.nextLine();
}
}
System.out.println("YOU DID IT!");
input.delimiter();
}
}