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

后端 未结 8 752
时光取名叫无心
时光取名叫无心 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条回答
  •  -上瘾入骨i
    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

提交回复
热议问题