我需要有关如何终止操作的意见(将空字符串解析为双精度)

时间:2019-03-14 17:05:15

标签: java user-interface javafx null numberformatexception

我正在编写一个ATM代码,但是我遇到一个简单的问题(希望),即当我单击“存款”按钮时,会弹出一个新窗口,上面有按钮(0到9),用户输入所需的金额,然后按“提交”,将标签中的文本解析为两倍,然后返回到存款方法,该方法将余额增加一倍(两倍)。问题是,当用户打开存款弹出窗口,然后通过单击X按钮将其关闭时,字符串返回一个空字符,这给我一个错误(NumberFormatException:空字符串),因为您无法将null解析为两倍。

我尝试了一个if语句,以判断字符串是否为空,将其设置为“ 0”,但随后的交易记录(一个字符串数组)存储了“ Deposit:0 $”,这是不正确的,因为他没有单击提交按钮(这也是不合逻辑的)。因此,我需要知道字符串是否为null,以便像终止操作并返回上一场景而又不将任何值返回给deposit方法。

这是返回代码

String value = labelNum.getText();
if(value == null || value.isEmpty()) { value = ""; }
return Double.valueOf(value);

这是它返回的方法:

  public void setDeposit(double deposit) { balance = balance + deposit; }

2 个答案:

答案 0 :(得分:0)

使用@FailingCoder,您可以在if语句中和setDeposit()函数中添加一些防御性编码,以便对deposit = 0语句进行代码检查。例如,

if(value != null && !value.isEmpty()){
    return Double.valueOf(value);
}
else
    return 0.0;

以及setDeposit()函数中

public void setDeposit(double deposit) 
{ 
    if(deposit > 0) //to avoid negative entries else != should also work
    balance = balance + deposit; 
}

答案 1 :(得分:0)

我会推荐一些类似的假设,假设您具有正确的正则表达式过滤掉字母减号,但是您的措辞听起来似乎不成问题,因为我只编码了1-9这样您就可以知道发生了什么,然后根据您的编码方式,不尝试返回null或0.0,您可以绑定帐户余额和标签,然后不必“刷新”我讨厌的标签,但这只是为了您可以了解解决此问题的其他方法

public class Main extends Application {

    private Label balanceLabel;
    private double accountBalance = 0.0;

    @Override
    public void start(Stage primaryStage) throws Exception {
        balanceLabel = new Label();
        setNewAccountBalanceLabel();

        Button depositButton = new Button("Deposit Money");
        depositButton.setOnAction(event -> depositAction());

        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        vBox.getChildren().addAll(balanceLabel, depositButton);

        Stage stage = new Stage();
        stage.setScene(new Scene(vBox));
        stage.show();
    }

    private void setNewAccountBalanceLabel(){ 
        balanceLabel.setText("Balance:"+accountBalance);
    }

    private void depositAction(){
        getDepositAmount();
        setNewAccountBalanceLabel();
    }

    private void getDepositAmount(){
        Stage stage  = new Stage();

        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);

        Label depositAmountLabel = new Label("0.00");

        TextField depositAmountTextField = new TextField();
        depositAmountTextField.setPromptText("Only Numbers");
        depositAmountTextField.setOnKeyReleased(keyEvent-> depositAmountLabel.setText(depositAmountTextField.getText()));

        Button submitButton = new Button("Submit");
        submitButton.setOnMouseClicked(event -> {
            double depositAmount = Double.parseDouble(depositAmountLabel.getText());
            accountBalance = accountBalance + depositAmount;
            stage.close();
        });

        vBox.getChildren().addAll(depositAmountLabel, depositAmountTextField, submitButton);

        stage.setScene(new Scene(vBox));
        stage.showAndWait();
    }

    public static void main(String[] args) { launch(args); }
}