令人困惑的数字格式异常
Posted
技术标签:
【中文标题】令人困惑的数字格式异常【英文标题】:Confusing number format exceptions 【发布时间】:2017-09-18 03:10:09 【问题描述】:我收到一大堆似乎源于我的解析双精度的异常,因为它们的信息是:线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:空字符串 另一个可能的嫌疑人是
account1.withdrawl(checkingBal, changeValue) ;
尽管我在被调用的方法中使用了 try catch 块,但确信这是一个未报告的异常:
public double withdrawl(double currentBal, double withdrawlAmount) throws InsufficientFunds
int i =0 ;
double overDraw = 0 ;
try
//verifies that input was numeric and evenly divisible by 20
if(withdrawlAmount % 20 == 0)
if(i>4)
if(currentBal>1.5)
currentBal = currentBal-1.5 ;
else
JOptionPane.showMessageDialog(null, "Insufficient funds");
overDraw = currentBal-1.5 ;
throw new InsufficientFunds(overDraw);
//stops withdrawl for insufficient funds
if(currentBal<withdrawlAmount)
JOptionPane.showMessageDialog(null, "Insufficient funds");
overDraw = currentBal-withdrawlAmount ;
throw new InsufficientFunds(overDraw);
else
currentBal=currentBal-withdrawlAmount ;
i++ ;
else
JOptionPane.showMessageDialog(null, "Please enter a withdarl amount that is divisible by 20");
catch (InsufficientFunds ex)
ex.getAmount();
return currentBal ;
public class ATM extends JFrame implements ActionListener
JButton withdraw, deposit, transfer, balance ;
JTextField input ;
boolean checkingActive = true ;
public double checkingBal = 0 ;
public double savingsBal = 0 ;
主类:
ATM()
withdraw = new JButton ("Withdraw") ;
deposit = new JButton ("Deposit") ;
transfer = new JButton ("Transfer") ;
balance = new JButton ("Balance") ;
input = new JTextField (12) ;
input.setText("0") ;
input.setEditable(true) ;
JRadioButton checking = new JRadioButton("Checking", true) ;;
JRadioButton savings = new JRadioButton("Savings") ;
this.add(checking);
this.add(savings);
ButtonGroup acctSelector = new ButtonGroup();
acctSelector.add(checking);
acctSelector.add(savings);
add(withdraw);
add(deposit);
add(transfer);
add(balance);
add(input);
//assigns buttons to logic gorup
ButtonGroup accountChoice = new ButtonGroup();
accountChoice.add(checking) ;
accountChoice.add(savings) ;
input = new JTextField(6) ;
//assigns listeners to buttons
withdraw.addActionListener(this) ;
deposit.addActionListener(this) ;
transfer.addActionListener(this) ;
balance.addActionListener(this) ;
setSize(500,400) ;
setLayout(new FlowLayout()) ;
setTitle("MoneyBank ATM") ;
//acceses account changes
public void actionPerformed(ActionEvent e)
double changeValue = Double.parseDouble(input.getText().trim());
Account account1 = new Account() ;
if(checkingActive==true)
if(e.getSource()== withdraw)
account1.withdrawl(checkingBal, changeValue) ;
else if(e.getSource()== deposit)
account1.deposit(checkingBal, changeValue) ;
else if(e.getSource()== transfer)
account1.transfer(checkingBal, changeValue) ;
else
account1.balance(checkingBal) ;
else
if(e.getSource()== withdraw)
account1.withdrawl(savingsBal, changeValue) ;
else if(e.getSource()== deposit)
account1.deposit(savingsBal, changeValue) ;
else if(e.getSource()== transfer)
account1.transfer(savingsBal, changeValue) ;
else
account1.balance(savingsBal) ;
JOptionPane.showMessageDialog(null, "Current balances are: \n Savings: " + savingsBal + "\n Checking: "+ checkingBal);
//constructs atm object/GUI
public static void main(String[] args)
ATM account=new ATM();
account.setVisible(true);
account.setLocation(200,200);
account.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public boolean pickAccount(boolean k)
checkingActive = k ;
return checkingActive ;
【问题讨论】:
当我在 withdawl 代码中加上 try - catchs 时,它并没有改变任何东西,所以我最好的猜测仍然是解析。但我有一个默认值,所以我不确定它为什么会出现问题 环顾四周时意识到我从来没有为我的单选按钮设置正确的操作命令,所以我会看看修复它是否有任何作用。编辑:它没有,但至少我解决了我的问题的那一部分 最好在执行此操作之前进行数据验证 Double.parseDouble(input.getText().trim()) 一个公平的观点,但我正在输入似乎可以接受的值,它仍然会触发异常 尝试在调用withdrawl()之前打印出输入的值,异常似乎不是来自withdrawl内部。异常本身是否会显示哪行代码抛出了错误? 【参考方案1】:您正在为变量输入创建 2 个不同的 JTextField 实例。
第一个:
input = new JTextField(12);
input.setText("0");
input.setEditable(true);
第二个:
input = new JTextField(6);
因此,当您在字段中写入一个数字并将该值发送到处理时,您收到的是一个空值“”。最后,您的应用程序尝试从该空导致生成 Double:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
您可以评论或删除第二个实例,您的应用程序将正常运行。
【讨论】:
以上是关于令人困惑的数字格式异常的主要内容,如果未能解决你的问题,请参考以下文章