I have a simple code that asks for the age of a user. Everything is working as it should, except, if I type nothing and simply press enter I enter into something - maybe infinite loop? I am not sure but I cannot get out of it.
I have seen countless questions and responses for this topic when dealing with strings but barely any with byte (or int).
This is my code:
//App #2: Age
System.out.print("Enter your Age: ");
byte age = scanner.nextByte();
while (!scanner.hasNext()) {
System.out.println("Please enter a valid number.");
byte test = scanner.nextByte();
}
if (age > 16) {
applicant.age = age;
System.out.println(age);
}
else
System.out.println("You must be at least 17 years old to obtain a microloan.");
What can I do to force a user to enter a valid number?