[英]Fixing result of Double.parseDouble is ignored error message
I have a utility class that checks to see if isNumeric or not by doing the following: 我有一个实用程序类,它通过执行以下操作来检查是否为isNumeric:
public static boolean isNumeric(String text) {
try {
Double.parseDouble(text);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
IntelliJ returns the following error: IntelliJ返回以下错误:
Warning:(30, 20) Result of 'Double.parseDouble()' is ignored 警告:(30,20)'Double.parseDouble()'的结果将被忽略
How can I refactor my code to avoid this error message? 如何重构代码以避免出现此错误消息? I don't need the result of Double.parseDouble()
to do anything and I don't want to also suppress the error message. 我不需要Double.parseDouble()
的结果来做任何事情,并且我也不想抑制错误消息。
Use a @SuppressWarnings
annotation on the method or the entire class: 在方法或整个类上使用@SuppressWarnings
批注:
@SuppressWarnings("UnusedReturnValue")
Or even better, don't reinvent the wheel and use the answers in this question instead! 甚至更好的是,不要重新发明轮子,而是使用这个问题的答案!
With mine , i don't have any mistakes , 有了我,我没有任何错误,
public boolean isNumber() {
try {
return !Double.isNaN(Double.parseDouble(this.answer));
} catch (Exception e) {
logger.error("Not a Number");
return false;
}
}
The method Double.isNaN(Your answer) makes the trick and is better than a SuppressWarning i think ;) Double.isNaN(您的答案)方法可以解决问题,并且比我认为的SuppressWarning更好;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.