Java - checking if parseInt throws exception

后端 未结 8 1293
清歌不尽
清歌不尽 2020-12-06 09:17

I\'m wondering how to do something only if Integer.parseInt(whatever) doesn\'t fail.

More specifically I have a jTextArea of user specified values seperated by line

8条回答
  •  生来不讨喜
    2020-12-06 09:38

    parseInt will throw NumberFormatException if it cannot parse the integer. So doing this will answer your question

    try{
    Integer.parseInt(....)
    }catch(NumberFormatException e){
    //couldn't parse
    }
    

提交回复
热议问题