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

后端 未结 8 753
时光取名叫无心
时光取名叫无心 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: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
    

提交回复
热议问题