Java - checking if parseInt throws exception

后端 未结 8 1283
清歌不尽
清歌不尽 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:44

    You can use a scanner instead of try-catch:

    Scanner scanner = new Scanner(line).useDelimiter("\n");
    if(scanner.hasNextInt()){
        System.out.println("yes, it's an int");
    }
    

提交回复
热议问题