I am new to java. First I want to check if the user input is String or Double or int. If it is a string, double or minus number, the user will be prompted to enter a valid int number again. Only when the user enters a real number, the program must jump to try. I thought for hours, and I came up with nothing useful. Please help, thanks!
import java.util.InputMismatchException; import java.util.Scanner; public class Fizz { public static void main(String[] args) { System.out.println("Please enter a number"); Scanner scan = new Scanner(System.in); try { Integer i = scan.nextInt(); if (i % 3 == 0 && (i % 5 == 0)) { System.out.println("FizzBuzz"); } else if (i % 3 == 0) { System.out.println("Fizz"); } else if (i % 5 == 0) { System.out.println("Buzz"); } else { System.out.println(i + "ใฏ3ใจ5ใฎๅๆฐใงใฏใใใพใใใ"); } } catch (InputMismatchException e) { System.out.println(""); } finally { scan.close(); } }
java java.util.scanner validation
user4799681
source share