我想知道只有在 Integer.parseInt(whatever) 没有失败的情况下如何做某事。
更具体地说,我有一个由换行符分隔的用户指定值的 jTextArea。
我想检查每一行是否可以转换为int。
想出了这样的东西,但它不起作用:
for(int i = 0; i < worlds.jTextArea1.getLineCount(); i++){
if(Integer.parseInt(worlds.jTextArea1.getText(worlds.jTextArea1.getLineStartOffset(i),worlds.jTextArea1.getLineEndOffset(i)) != (null))){}
}
任何帮助表示赞赏。
最佳答案
public static boolean isParsable(String input) {
try {
Integer.parseInt(input);
return true;
} catch (final NumberFormatException e) {
return false;
}
}
关于Java - 检查 parseInt 是否抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6456219/