Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to judge whether the input data is int or string in java? Please point out the following mistakes, thank you.
How to judge whether the input data is int or string in java? Please point out the following mistakes, thank you.
Hello, this problem can be solved with the help of java regular expression features. First, use the Scanner instance object to get the next string, assuming that it is stored in String inputStr for the time being. A regular expression of type int can be represented by the following string:

String regStr = "^[+- "? [ 1-9][0-9]*$|^0$";

The following if statement can be used to judge integers:

if(inputStr.match(regStr)){

system . out . println(" integer ");

} Otherwise {

System.out.println ("non-integer");

}

Finally, I sincerely hope to learn something!