java to check if its a number scanner

Solutions on MaxInterview for java to check if its a number scanner by the best coders in the world

showing results for - "java to check if its a number scanner"
Etan
23 Feb 2020
1Scanner sc = new Scanner(System.in);
2int number;
3do {
4    System.out.println("Please enter a positive number!");
5    while (!sc.hasNextInt()) {
6        System.out.println("That's not a number!");
7        sc.next(); // this is important!
8    }
9    number = sc.nextInt();
10} while (number <= 0);
11System.out.println("Thank you! Got " + number);