how to check if an input is an integer java
import java.util.InputMismatchException;
import java.util.Scanner;
public class Number {
public static void main(String[] args) {
int yourNumber;
while(true) {
// to read inputs from user
Scanner scan = new Scanner(System.in);
try {
System.out.print("ayy bruh enter you age : ");
// throws InputMismatchException if it's not a number
yourNumber = scan.nextInt();
scan.close();
break;
}catch(InputMismatchException notNumber) { // << catch the exception and print the error message
System.err.println("huh I can understand numbers,"
+ "ENTER A NUMBER !");
}
}
// print the result :))
System.out.printf("so your age is %d, ..AWESOME !! :))", yourNumber);
}
}