Try-Catch异常Java-Eclipse

Try-Catch异常Java-Eclipse,java,try-catch,Java,Try Catch,我想在Java中使用这个try/catch,在程序中使用Execption。基本上,该程序的目标是获得某一产品的数量/数量,并计算其价格和折扣 我想要txtmount,如果将字母放在这里是代码的一部分,它应该只接收数字,不会崩溃。只是一个简单的解决方案 public static String modelo, obsequio = null, resolucion = null; public static int amount, numgift = 0, resolution = 0;

我想在Java中使用这个try/catch,在程序中使用Execption。基本上,该程序的目标是获得某一产品的数量/数量,并计算其价格和折扣

我想要
txtmount
,如果将字母放在这里是代码的一部分,它应该只接收数字,不会崩溃。只是一个简单的解决方案

public static String modelo, obsequio = null, resolucion = null;
    public static int amount, numgift = 0, resolution = 0;
    public static double amountourchase = 0, idiscount = 0, itopayr = 0, discount = 0, price = 0;

    protected void actionPerformedBtnSell(ActionEvent arg0) {
        inData();
        calculateDiscount();
        prrocessPurchase();
        Results();
    }

    void indata(){
        model = cboModelo.getSelectedItem().toString();
        amount = Integer.parseInt(txtCantidad.getText());
    }
代码:

注意:试着解决这个问题,我认为下面的代码就是您正在寻找的代码

代码:

public static void main(String[] args) {
        JFrame f = new JFrame("Demo");
        f.setLayout(new FlowLayout());
        f.setSize(300, 200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        JLabel label = new JLabel("enter your number: ");
        JTextField userText = new JTextField(6);
        JButton button = new JButton("OK");
        JLabel label1 = new JLabel();

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    int i = Integer.parseInt(userText.getText());
                    label1.setText(Integer.toString(i));
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(f,
                            "Input integer number ",
                            "NumberFormatException",
                            JOptionPane.ERROR_MESSAGE);
                    userText.setText("");
                }
            }
        });

        f.add(label);
        f.add(userText);
        f.add(button);
        f.add(label1);
        f.setVisible(true);
    }
注意
您应该在类型double中定义您的金额和其他变量,使其更精确

在int readNumber(){int num;try{num=Integer.parseInt(txtNumero.getText());}catch(异常e){num=-1;return num;{.ABOVEI刚刚完成键入,是否可以在代码中实现这是一个示例Apart如何在int readNumber()上面的代码中使用这个新示例{int num;try{num=Integer.parseInt(txtnumeo.getText());}catch(异常e){num=-1;return num;{。有可能在上面的代码中实现吗?让我看看你想说什么?你想完成什么?因为当你在catch vlock中时,你返回了一些东西。所有东西都不见了。你想让这个人在循环中,直到他或她输入正确的格式吗?是的,因为如果这个人继续输入字母和数字
try {
        double d = Double.parseDouble(your input is here);
         or
        int i =  Integer.parseInt(your input is here);
    }
    catch (NumberFormatException e) {
         System.out.print(e);

    }
public static void main(String[] args) {
        JFrame f = new JFrame("Demo");
        f.setLayout(new FlowLayout());
        f.setSize(300, 200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        JLabel label = new JLabel("enter your number: ");
        JTextField userText = new JTextField(6);
        JButton button = new JButton("OK");
        JLabel label1 = new JLabel();

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    int i = Integer.parseInt(userText.getText());
                    label1.setText(Integer.toString(i));
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(f,
                            "Input integer number ",
                            "NumberFormatException",
                            JOptionPane.ERROR_MESSAGE);
                    userText.setText("");
                }
            }
        });

        f.add(label);
        f.add(userText);
        f.add(button);
        f.add(label1);
        f.setVisible(true);
    }