How do I validate if user input is a number? The number is from the main method and it's checked if it's a number in another method. It should return true if the input is a number and false if it's not.
What I have so far:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Please, enter a number: ");
int number = sc.nextInt();
validateInput(number);
}
public static int validateInput(int num) {
while(num <= 0)
if(!sc.hasNextInt())
System.out.print("INVALID INPUT, Try again...");
}
Output should be:
Please, enter a number: a
Invalid input, Try again...
Please, enter a number: 16
Valid input, thank you.