How to check if user input is a string double or long in Java

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(); } } 
+7
java java.util.scanner validation
source share
5 answers

One simple fix is โ€‹โ€‹to read the entire string / user input as a string. Something like this should work. (Unused code):

  String s=null; boolean validInput=false; do{ s= scannerInstance.nextLine(); if(s.matches("\\d+")){// checks if input only contains digits validInput=true; } else{ // invalid input } }while(!validInput); 
+8
source share

You can also use Integer.parseInt and then verify that the integer is not negative. You can catch a NumberFormatException if the input is a string or double.

 Scanner scan = new Scanner(System.in); try { String s = scan.nextLine(); int x = Integer.parseInt(s); } catch(NumberFormatException ex) { } 
+1
source share

Try it. I used some conditions to indicate input.

 Scanner scan = new Scanner(System.in); String input = scan.nextLine(); int charCount = input.length(); boolean flag = false; for(int x=0; x<charCount; x++){ for(int y=0; y<10; y++){ if(input.charAt(x)==Integer.toString(y)) flag = true; else{ flag = false; break; } } } if(flag){ if(scan.hasNextDouble()) System.out.println("Input is Double"); else System.out.println("Input is Integer"); } else System.out.println("Invalid Input. Please Input a number"); 
0
source share

Try it. It will ask for input until an int value greater than 0 is entered:

 System.out.println("Please enter a number"); try (Scanner scan = new Scanner(System.in)) { while (scan.hasNext()) { int number; if (scan.hasNextInt()) { number = scan.nextInt(); } else { System.out.println("Please enter a valid number"); scan.next(); continue; } if (number < 0) { System.out.println("Please enter a number > 0"); continue; } //At this stage, the number is an int >= 0 System.out.println("User entered: " + number); break; } } 
0
source share
 boolean valid = false; double n = 0; String userInput = ""; Scanner input = new Scanner(System.in); while(!valid){ System.out.println("Enter the number: "); userInput = input.nextLine(); try{ n = Double.parseDouble(userInput); valid = true; } catch (NumberFormatException ex){ System.out.println("Enter the valid number."); } } 
0
source share

All Articles