Java货币转换器应用程序中的异常处理

时间:2018-03-05 11:02:10

标签: java exception-handling

我的代码运行正常,应用程序运行正常,除非我故意输入错误的数据,期望处理错误。我在网上搜索过,但实际上无法弄清楚为什么我的代码中的错误没有得到处理。 在控制台中打印出的第一行异常是:线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“as”

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyFrame extends JFrame implements ActionListener{
    public static final long serialVersionUID = 1L;

    JPanel panel1, panel2, panel3;
    JLabel cedi_tag, value_equivalent;
    JTextField txtCedi, txtResults;
    JButton btn_usd, btn_euro, btn_pounds;
    JMenuBar mainMenu = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem exitItem = new JMenuItem("Exit...");

    MyFrame(String title){
        super(title);
        setSize(300, 300);
        setLocation(400, 200);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        fileMenu.add(exitItem);
        mainMenu.add(fileMenu);
        exitItem.addActionListener(this);
        this.setJMenuBar(mainMenu);

        cedi_tag = new JLabel("Enter GHC");
        panel1 = new JPanel();
        panel1.add(cedi_tag);
        getContentPane().add(panel1);

        txtCedi = new JTextField(15);
        panel1.add(txtCedi);

        value_equivalent = new JLabel("Result");
        panel2 = new JPanel();
        panel2.add(value_equivalent);
        getContentPane().add(panel2, BorderLayout.SOUTH);

        txtResults = new JTextField(15);
        txtResults.setEditable(false);
        panel2.add(txtResults);

        btn_usd = new JButton("Dollars");
        btn_usd.setActionCommand("dollars");
        btn_usd.addActionListener(this);
        panel3 = new JPanel();
        panel3.add(btn_usd);
        getContentPane().add(panel3);

        btn_pounds = new JButton("Pounds");
        btn_pounds.addActionListener(this);
        btn_pounds.setActionCommand("pounds");
        panel3.add(btn_pounds);

        btn_euro = new JButton("Euros");
        btn_euro.addActionListener(this);
        btn_euro.setActionCommand("euros");
        panel3.add(btn_euro);
    }

    public void actionPerformed(ActionEvent event){
        String strCedi = txtCedi.getText();
        double cedi_value = Double.parseDouble(strCedi);

        if(event.getActionCommand().equals("dollars")){
            try{
                double answer = Converter.CedisToDollars(cedi_value);
                txtResults.setText(String.format("%.2f", answer));
            }

            catch(Exception e){
                txtResults.setText("Please enter a number");
            }
        }

        if(event.getActionCommand().equals("pounds")){
            try{
                double answer = Converter.CedisToPounds(cedi_value);
                txtResults.setText(String.format("%.2f", answer));
            }

            catch(Exception e){
                txtResults.setText("Please enter a valid amount");
            }

        }

        if(event.getActionCommand().equals("euros")){
            try{
                double answer = Converter.CedisToEuros(cedi_value);
                txtResults.setText(String.format("%.2f", answer));
            }

            catch(Exception ex){
                txtResults.setText("Please Enter a valid amount");
            }

        }
        exitItem.setActionCommand("exit");
        }

    }

class Converter{
    public static double CedisToDollars(double cedis){
        double dollar_value = cedis * 4.2;
        return dollar_value;
    }

    public static double CedisToPounds(double cedis){
        double pounds_value = cedis * 5.1;
        return pounds_value;
    }

    public static double CedisToEuros(double cedis){
        double euros_value = cedis * 4.6;
        return euros_value;
    }
}

public class CurrencyConverter {

    public static void main(String[] args) {
        MyFrame master = new MyFrame("Currency Converter");
        master.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:0)

例外来自:

Double.parseDouble(strCedi);

这是在你的try-catch块之外。

在其周围放置一个try-catch块并添加一些处理代码。