How can I check if an input is a integer or String, etc.. in JAVA?

后端 未结 8 746
时光取名叫无心
时光取名叫无心 2020-12-18 14:41

I am wondering how I can check if a user\'s input is a certain primitive type (I mean integer, String, etc... I think it\'s called primitive type?). I want a user to input s

相关标签:
8条回答
  • 2020-12-18 15:17

    You may try this with Regex:

    String input = "34";
    if(input.matches("^\\d+(\\.\\d+)?")) {
      //okay
    } else {
      // not okay !
    }
    

    Here,

    ^\\d+ says that input starts with a digit 0-9,

    ()? may/or may not occur

    \\. allows one period in input

    0 讨论(0)
  • 2020-12-18 15:19
    Scanner input = new Scanner (System.in);
    
    if (input.hasNextInt()) System.out.println("This input is of type Integer.");
    else if (input.hasNextFloat()) System.out.println("This input is of type Float.");
    else if (input.hasNextLine()) System.out.println("This input is of type string."); 
    else if (input.hasNextDouble()) System.out.println("This input is of type Double."); 
    else if (input.hasNextBoolean()) System.out.println("This input is of type Boolean.");  
    
      else if (input.hasNextLong())
        System.out.println("This input is of type Long."); 
    
    0 讨论(0)
  • 2020-12-18 15:23

    Hate to bring this up after 6 years but I found another possible solution.

    Currently attending a coding bootcamp and had to solve a similar problem. We introduce booleans and change their values depending on the result of the try/catch blocks. We then check the booleans using simple if statements. You can omit the prints and input your code instead. Here's what it looks like:

    import java.io.IOException;
    import java.util.Scanner;
    
    public class DataTypeFinder {
        public static void main(String[] args) throws IOException {
            Scanner scan = new Scanner(System.in);
            String input = "";
    
            while (true) { //so we can check multiple inputs (optional)
                input = scan.nextLine();
    
                if ("END".equals(input)) { //a way to exit the loop
                    break;
                }
    
                boolean isInt = true; //introduce boolean to check if input is of type Integer
                try { // surround with try/catch
                    int integer = Integer.parseInt(input); //boolean is still true if it works
                } catch (NumberFormatException e) {
                    isInt = false; //changed to false if it doesn't
                }
    
                boolean isDouble = true; //same story
                try {
                    double dbl = Double.parseDouble(input);
                } catch (NumberFormatException e) {
                    isDouble = false;
                }
    
                if (isInt) {
                    System.out.printf("%s is integer type%n", input);
                } else if (isDouble) {
                    System.out.printf("%s is floating point type%n", input);
                } else if (input.length() == 1) { //this could be useless depending on your case
                    System.out.printf("%s is character type%n", input);
                } else if ("true".equals(input.toLowerCase()) || "false".equals(input.toLowerCase())) {
                    System.out.printf("%s is boolean type%n", input);
                } else {
                    System.out.printf("%s is string type%n", input);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-18 15:25
    class Main{
        public static void main(String args[]){
            String str;
            Scanner sc=new Scanner(System.in);
            int n,
            boolean flag=false;
            while(!flag){
                try{
                    str=sc.nextLine();
                    n=Integer.parseInt(str);
                    flag=true;
                }
                catch(NumberFormatException e){
                    System.out.println("enter an no");
                    str=sc.nextLine();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-18 15:25

    Try instanceof function with Integer instead of int.. each primitive also have a class

    0 讨论(0)
  • 2020-12-18 15:28

    Is this ok?

    class Test
    {
        public static void main(String args[])
        {
            java.util.Scanner in = new java.util.Scanner(System.in);
            String x = in.nextLine();
            System.out.println("\n The type of the variable is : "+x.getClass());
        }
    }
    

    Output:

    subham@subham-SVE15125CNB:~/Desktop$ javac Test.java 
    subham@subham-SVE15125CNB:~/Desktop$ java Test
    hello
    
     The type of the variable is : java.lang.String
    
    0 讨论(0)
提交回复
热议问题