Java - 在 'catch' 块中为 Double.parseDouble() 包含负值
Java - include negative values in 'catch' block for Double.parseDouble()
我有一个 Java 客户端应用程序可以向服务器发送消息。我正在设置输入验证,以便客户端检测是否在 'send' 按钮单击时发送了可接受的消息。
应该接受的消息是任何可以解析为双精度或多个选定字符串(由 string.equals()
方法识别)的消息。
我首先测试字符串,如果不满足条件,我尝试将消息解析为 double (Double.parseDouble(message)
)。在这一点上,我有一个 try/catch
块,它识别任何失败的尝试解析为 double(意味着它必须包含字母,因此无效),它捕获 NumberFormatException
并允许用户有机会重新-输入消息。
我现在想使数字不能为负数,并希望将其包含在同一个 try/catch
块中,以便任何负值都可以为用户提供相同的机会重新 -输入。
这是我目前拥有的:
else
{
try
{
//convert number to double (monetary value)
bidAmount = Double.parseDouble(messageToServer);
//send to server with item code
output.println(selectedItemCode + bidAmount);
}
//item cannot turned to double
catch (NumberFormatException numfEx)
{
//inform user
fromServer.setText("Invalid Request!"
+ USER_PROMPT);
}
}
//clear input field for subsequent entry
toServer.setText("");
任何人都可以指出如何实施此规则的方向,尽可能避免代码重复吗?
谢谢,
马克
在bidAmount = Double.parseDouble(messageToServer);
之后,我们需要添加以下内容:
if(bidAmount < 0){
throw new NumberFormatException("Invalid amount");
}
另一种解决方案(可能也是最佳实践)是修改 catch 块以捕获 IllegalArgumentException
并在金额小于 0 时抛出 IllegalArgumentException
,如下所示:
try
{
//convert number to double (monetary value)
bidAmount = Double.parseDouble(messageToServer);
if(bidAmount < 0){
throw new IllegalArgumentException("Invalid amount");
}
//send to server with item code
output.println(selectedItemCode + bidAmount);
}
//item cannot turned to double
catch (IllegalArgumentException numfEx)
{
//inform user
fromServer.setText("Invalid Request!"
+ USER_PROMPT);
}
创建一个单独的方法,将 String 解析为 Double,然后检查它是否为非负数,return在任一无效情况下为 null。
//convert number to double (monetary value)
bidAmount = getDouble();
if(bidAmount != null) {
//send to server with item code
output.println(selectedItemCode + bidAmount);
toServer.setText("");
} else {
//item cannot turned to double
//inform user
fromServer.setText("Invalid Request!"
+ USER_PROMPT);
}
private Double getDouble() {
try {
double d = Double.parseDouble(messageToServer);
if(d >= 0) {
return d;
}
} catch (NumberFormatException numfEx) {}
return null;
}
编辑:如果您反对使用空值,您也可以将方法更改为 return 默认负值,例如 -1,然后更改条件以检查双精度是否为负而不是空值。
这可以在不依赖异常来控制程序流程的情况下实现,这通常是个坏主意 - 请参阅讨论 here
else {
// using a tempBidAmount as I don't have enough context to know whether
// I could just assign the output of parseUserDouble directly to bidAmount
// without causing issues
Double tempBidAmount = parseUserDouble(messageToServer);
if (tempBidAmount != null && tempBidAmount > 0.0) {
bidAmount = tempBidAmount;
output.println(selectedItemCode + bidAmount);
} else {
fromServer.setText("Invalid Request!" + USER_PROMPT);
}
}
toServer.setText("");
....
private Double parseUserDouble(Sting userInput) {
try {
return Double.parseDouble(userInput);
}
catch (NumberFormatException numfEx) {
// consider logging here
}
return null;
}
我有一个 Java 客户端应用程序可以向服务器发送消息。我正在设置输入验证,以便客户端检测是否在 'send' 按钮单击时发送了可接受的消息。
应该接受的消息是任何可以解析为双精度或多个选定字符串(由 string.equals()
方法识别)的消息。
我首先测试字符串,如果不满足条件,我尝试将消息解析为 double (Double.parseDouble(message)
)。在这一点上,我有一个 try/catch
块,它识别任何失败的尝试解析为 double(意味着它必须包含字母,因此无效),它捕获 NumberFormatException
并允许用户有机会重新-输入消息。
我现在想使数字不能为负数,并希望将其包含在同一个 try/catch
块中,以便任何负值都可以为用户提供相同的机会重新 -输入。
这是我目前拥有的:
else
{
try
{
//convert number to double (monetary value)
bidAmount = Double.parseDouble(messageToServer);
//send to server with item code
output.println(selectedItemCode + bidAmount);
}
//item cannot turned to double
catch (NumberFormatException numfEx)
{
//inform user
fromServer.setText("Invalid Request!"
+ USER_PROMPT);
}
}
//clear input field for subsequent entry
toServer.setText("");
任何人都可以指出如何实施此规则的方向,尽可能避免代码重复吗?
谢谢, 马克
在bidAmount = Double.parseDouble(messageToServer);
之后,我们需要添加以下内容:
if(bidAmount < 0){
throw new NumberFormatException("Invalid amount");
}
另一种解决方案(可能也是最佳实践)是修改 catch 块以捕获 IllegalArgumentException
并在金额小于 0 时抛出 IllegalArgumentException
,如下所示:
try
{
//convert number to double (monetary value)
bidAmount = Double.parseDouble(messageToServer);
if(bidAmount < 0){
throw new IllegalArgumentException("Invalid amount");
}
//send to server with item code
output.println(selectedItemCode + bidAmount);
}
//item cannot turned to double
catch (IllegalArgumentException numfEx)
{
//inform user
fromServer.setText("Invalid Request!"
+ USER_PROMPT);
}
创建一个单独的方法,将 String 解析为 Double,然后检查它是否为非负数,return在任一无效情况下为 null。
//convert number to double (monetary value)
bidAmount = getDouble();
if(bidAmount != null) {
//send to server with item code
output.println(selectedItemCode + bidAmount);
toServer.setText("");
} else {
//item cannot turned to double
//inform user
fromServer.setText("Invalid Request!"
+ USER_PROMPT);
}
private Double getDouble() {
try {
double d = Double.parseDouble(messageToServer);
if(d >= 0) {
return d;
}
} catch (NumberFormatException numfEx) {}
return null;
}
编辑:如果您反对使用空值,您也可以将方法更改为 return 默认负值,例如 -1,然后更改条件以检查双精度是否为负而不是空值。
这可以在不依赖异常来控制程序流程的情况下实现,这通常是个坏主意 - 请参阅讨论 here
else {
// using a tempBidAmount as I don't have enough context to know whether
// I could just assign the output of parseUserDouble directly to bidAmount
// without causing issues
Double tempBidAmount = parseUserDouble(messageToServer);
if (tempBidAmount != null && tempBidAmount > 0.0) {
bidAmount = tempBidAmount;
output.println(selectedItemCode + bidAmount);
} else {
fromServer.setText("Invalid Request!" + USER_PROMPT);
}
}
toServer.setText("");
....
private Double parseUserDouble(Sting userInput) {
try {
return Double.parseDouble(userInput);
}
catch (NumberFormatException numfEx) {
// consider logging here
}
return null;
}