Java - 检查 parseInt 是否抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6456219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - checking if parseInt throws exception
提问by Mike Haye
I'm wondering how to do something only if Integer.parseInt(whatever) doesn't fail.
我想知道只有在 Integer.parseInt(whatever) 没有失败的情况下才能做些什么。
More specifically I have a jTextArea of user specified values seperated by line breaks.
更具体地说,我有一个由换行符分隔的用户指定值的 jTextArea。
I want to check each line to see if can be converted to an int.
我想检查每一行以查看是否可以转换为 int。
Figured something like this, but it doesn't work:
想出这样的事情,但它不起作用:
for(int i = 0; i < worlds.jTextArea1.getLineCount(); i++){
if(Integer.parseInt(worlds.jTextArea1.getText(worlds.jTextArea1.getLineStartOffset(i),worlds.jTextArea1.getLineEndOffset(i)) != (null))){}
}
Any help appreciated.
任何帮助表示赞赏。
采纳答案by fmucar
public static boolean isParsable(String input) {
try {
Integer.parseInt(input);
return true;
} catch (final NumberFormatException e) {
return false;
}
}
回答by Amir Raminfar
parseInt will throw NumberFormatExceptionif it cannot parse the integer. So doing this will answer your question
如果parseInt无法解析整数,则会抛出NumberFormatException。所以这样做会回答你的问题
try{
Integer.parseInt(....)
}catch(NumberFormatException e){
//couldn't parse
}
回答by Yet Another Geek
You can use the try..catch statementin java, to capture an exception that may arise from Integer.parseInt().
您可以使用java 中的try..catch 语句来捕获可能由 Integer.parseInt() 引起的异常。
Example:
例子:
try {
int i = Integer.parseint(stringToParse);
//parseInt succeded
} catch(Exception e)
{
//parseInt failed
}
回答by Stefan Bossbaly
It would be something like this.
它会是这样的。
String text = textArea.getText();
Scanner reader = new Scanner(text).useDelimiter("\n");
while(reader.hasNext())
String line = reader.next();
try{
Integer.parseInt(line);
//it worked
}
catch(NumberFormatException e){
//it failed
}
}
回答by Kerem Baydo?an
Check if it is integer parseable
检查它是否是整数可解析的
public boolean isInteger(String string) {
try {
Integer.valueOf(string);
return true;
} catch (NumberFormatException e) {
return false;
}
}
or use Scanner
或使用扫描仪
Scanner scanner = new Scanner("Test string: 12.3 dog 12345 cat 1.2E-3");
while (scanner.hasNext()) {
if (scanner.hasNextDouble()) {
Double doubleValue = scanner.nextDouble();
} else {
String stringValue = scanner.next();
}
}
or use Regular Expressionlike
或使用正则表达式,如
private static Pattern doublePattern = Pattern.compile("-?\d+(\.\d*)?");
public boolean isDouble(String string) {
return doublePattern.matcher(string).matches();
}
回答by Anantha Sharma
instead of try
ing & catch
ing expressions.. its better to run regex on the string to ensure that it is a valid number..
而不是try
ing & catch
ing 表达式.. 最好在字符串上运行正则表达式以确保它是一个有效数字..
回答by dogbane
You can use a scanner instead of try-catch:
您可以使用扫描仪代替 try-catch:
Scanner scanner = new Scanner(line).useDelimiter("\n");
if(scanner.hasNextInt()){
System.out.println("yes, it's an int");
}
回答by Siddhartha Ghosh
You could try
你可以试试
NumberUtils.isParsable(yourInput)
It is part of org/apache/commons/lang3/math/NumberUtils
and it checks whether the string can be parsed by Integer.parseInt(String)
, Long.parseLong(String)
, Float.parseFloat(String)
or Double.parseDouble(String)
.
这是的一部分org/apache/commons/lang3/math/NumberUtils
和它检查是否该字符串可以通过进行解析Integer.parseInt(String)
,Long.parseLong(String)
,Float.parseFloat(String)
或Double.parseDouble(String)
。
See below:
见下文: