我得到了一大堆异常似乎源于我的解析双,因为它们的信息是:线程中的异常“AWT-EventQueue-0”java.lang.NumberFormatException:empty String 另一个可能的嫌疑人是
account1.withdrawl(checkingBal, changeValue) ;
尽管我在调用方法中尝试了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 ;
}
}
答案 0 :(得分:0)
您正在为变量输入创建2个不同的JTextField实例。
第一个:
input = new JTextField(12);
input.setText("0");
input.setEditable(true);
第二个:
input = new JTextField(6);
所以,当你在字段中写一个数字并将值发送到进程时,你收到的是一个空值&#34;&#34;。最后,您的应用程序尝试从该空生成Double:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
您可以评论或删除第二个实例,您的应用程序也可以使用。