令人困惑的数字格式异常
Confusing number format exceptions
我收到一大堆异常,这些异常似乎源于我的解析双精度,因为它们的信息是:线程异常 "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 ;
主要class:
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 ;
}
}
您正在为变量输入创建 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
您可以评论或删除第二个实例,您的应用程序将正常运行。
我收到一大堆异常,这些异常似乎源于我的解析双精度,因为它们的信息是:线程异常 "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 ;
主要class:
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 ;
}
}
您正在为变量输入创建 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
您可以评论或删除第二个实例,您的应用程序将正常运行。