Java 通过Try-and-Catch块检查字符串和Null异常
以下试图通过日志记录捕获empity double inputvia的代码不起作用。我还想检查用户是否错误地在输入字段中添加了字符串,但我在编写查询时遇到了问题。有人能看看这个并指导我吗Java 通过Try-and-Catch块检查字符串和Null异常,java,logging,exception-handling,log4j,double,Java,Logging,Exception Handling,Log4j,Double,以下试图通过日志记录捕获empity double inputvia的代码不起作用。我还想检查用户是否错误地在输入字段中添加了字符串,但我在编写查询时遇到了问题。有人能看看这个并指导我吗 try { if (results1 != 00.00) { throw new Exception("Data must be added"); } } catch (Exception e) { log.error("You have an error", e);
try {
if (results1 != 00.00) {
throw new Exception("Data must be added");
}
}
catch (Exception e) {
log.error("You have an error", e);
}
用户必须为用于计算的results1输入一个值。结果1是一个双倍,因为它是一个百分比,我愿意将其更改为int,如果这是使其工作所需要的。如果用户不小心添加了%符号,我还想检查一下try-and-catch技术。我希望我的记录器捕获NumberFormatException以进行测试
我认为这可能是问题所在:
(results1 != 00.00)
这是检查双精度输入是否为空的最佳方法。另外,如何检查是否添加了字符串?使用“==”或“!=”不是一个好主意对于双人,浮动。你可以用非常小的数字来代替等号“==”
Math.abs(result1) > 0.001
使用“==”或“!=”不是一个好主意对于双人,浮动。你可以用非常小的数字来代替等号“==”
Math.abs(result1) > 0.001
Java具有Double.parseDoublestring函数,如果无法将字符串转换为Double,它将抛出NumberFormatException //编辑 结合prashant的回答,这里是一个完整的实现
try {
Double result = Double.parseDouble(string);
//This is required to check that number is a valid Percentage value
if(!(result > 0 && result < 100)){
//You will have to create this custom exception or throw a simple exception
throw new InvalidPercentageCustomException(result + " is not a valid Percentage");
}
}
catch(Exception e){
//Do Something
}
Java具有Double.parseDoublestring函数,如果无法将字符串转换为Double,它将抛出NumberFormatException //编辑 结合prashant的回答,这里是一个完整的实现
try {
Double result = Double.parseDouble(string);
//This is required to check that number is a valid Percentage value
if(!(result > 0 && result < 100)){
//You will have to create this custom exception or throw a simple exception
throw new InvalidPercentageCustomException(result + " is not a valid Percentage");
}
}
catch(Exception e){
//Do Something
}
据我所知,您希望检查用户输入的字符串是否为有效百分比 您可以像这样使用parseDouble:
try{
Double result = Double.parseDouble(results);
//This is required to check that number is a valid Percentage value
if(result <= 0.0 || result >= 100.0){
//You will have to create this custom exception or throw a simple exception
throw new InvalidPercentageCustomException("Not a valid Percentage");
}
}
catch(Exception e){
//Do Something
}
还有一条建议:您不应该仅仅为了让程序正常工作而更改数据类型。据我所知,您希望检查用户输入的字符串是否为有效百分比 您可以像这样使用parseDouble:
try{
Double result = Double.parseDouble(results);
//This is required to check that number is a valid Percentage value
if(result <= 0.0 || result >= 100.0){
//You will have to create this custom exception or throw a simple exception
throw new InvalidPercentageCustomException("Not a valid Percentage");
}
}
catch(Exception e){
//Do Something
}
还有一条建议:你不应该仅仅为了让你的程序工作而改变你的数据类型。这就是你必须检查NumberFormatException的方法。下面的程序有三个例子。第一个是成功场景,第二个是带parse异常的空字符串,第三个是带parse异常的字符串
package com.stackoverflow.test;
public class ParseCheck {
public static void main(String args[]) {
String results1 = "123.56";
String results2 = "";
String results3 = "xyz";
try {
Double.parseDouble(results1);
System.out.println(results1 + " parsed successfully");
} catch (NumberFormatException e) {
e.printStackTrace();
}
try {
Double.parseDouble(results2);
System.out.println(results2 + "parsed successfully");
} catch (NumberFormatException e) {
e.printStackTrace();
}
try {
Double.parseDouble(results3);
System.out.println(results3 + "parsed successfully");
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
这就是检查NumberFormatException的方法。下面的程序有三个例子。第一个是成功场景,第二个是带parse异常的空字符串,第三个是带parse异常的字符串
package com.stackoverflow.test;
public class ParseCheck {
public static void main(String args[]) {
String results1 = "123.56";
String results2 = "";
String results3 = "xyz";
try {
Double.parseDouble(results1);
System.out.println(results1 + " parsed successfully");
} catch (NumberFormatException e) {
e.printStackTrace();
}
try {
Double.parseDouble(results2);
System.out.println(results2 + "parsed successfully");
} catch (NumberFormatException e) {
e.printStackTrace();
}
try {
Double.parseDouble(results3);
System.out.println(results3 + "parsed successfully");
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
results1是什么类型的?result1是双精度的。但如果需要,我可以将其更改为int。results1是什么类型?result1是双精度的。但如果需要,我可以将其更改为int。为什么>0.001,而不是>=0.01?这就是重点。这取决于result1的精度。无论如何+10.01或0.001并不重要。双精度和浮点精度都有更高的精度。如果结果是0.002,而不是0,由于前面步骤中的精度损失,我们谈论的是货币,那么如果你认为它等于零,那么它会产生巨大的差异。为什么>0.001,而不是>0.01?这就是重点。这取决于result1的精度。无论如何+10.01或0.001并不重要。双精度和浮点精度都有更高的精度。如果结果是0.002,而不是0,由于前面步骤中的精度损失,我们谈论的是货币,那么如果你认为它等于零,那么它会产生巨大的差异。我们可以捕获NullPointerException和NumberFormatException,因为parseDouble只抛出其中一个。这提高了可读性,使我们能够实现更好的异常处理。不需要捕获顶级异常e。我们可以捕获NullPointerException和NumberFormatException,因为parseDouble只抛出其中一个。这提高了可读性,使我们能够实现更好的异常处理。