java - 无法解析 Integer.ParseInt 抛出的 java.lang.NumberFormatException

标签 java user-interface parseint numberformatexception

我收到此错误,但我似乎找不到可能导致它的原因。它说错误在我初始化变量 bet 的那一行。如果我删除该行和 Integer.parseInt(betTextFieldAmount),那么它会说错误发生在初始化上。

public class RollTheDice extends JFrame implements ActionListener {

    JButton roll = new JButton("Roll the Dice");
    JButton bet1 = new JButton("5$");
    JButton bet2 = new JButton("10$");
    JButton bet3 = new JButton("25$");
    JButton bet4 = new JButton("50$");
    JButton bet5 = new JButton("100$");

    JTextField betTextField = new JTextField(5);
    String betTextFieldAmount = betTextField.getText();
    int bet = Integer.parseInt(betTextFieldAmount);

    int money = 100;

    JLabel currentBet = new JLabel();
    JLabel currentMoney = new JLabel("Your money: " + money + "$");

    int myDice = new Random().nextInt(12) + 1;
    int AIDice = new Random().nextInt(12) + 1;

    public static void RollTheDice() {
        int myDice = new Random().nextInt(12) + 1;
        int AIDice = new Random().nextInt(12) + 1;
    }

    public RollTheDice() {
        setLayout(new FlowLayout());

        add(currentMoney);

        add(bet1);
        bet1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 5) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 5;
                    bet +=5;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(bet2);
        bet2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 10) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 10;
                    bet +=10;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(bet3);
        bet3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 25) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 25;
                    bet +=25;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(bet4);
        bet4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 50) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 50;
                    bet +=50;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(bet5);
        bet5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (money < 100) {
                    JOptionPane.showMessageDialog(null, "Not enough money!");
                } else {
                    money -= 100;
                    bet +=100;
                }
                currentMoney.setText(money + "$");
            }           
        });

        add(betTextField);
        betTextField.setEditable(false);
        add(roll);
        roll.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (myDice > AIDice) {
                    money += bet;
                    JOptionPane.showMessageDialog(null, "You won " + bet + "$");
                } else {
                    JOptionPane.showMessageDialog(null, "You lost " + bet + "$");
                }
                currentMoney.setText(money + "$");
                RollTheDice();
            }           
        });
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

最佳答案

这是由于 betTextFieldAmount 的值不是有效整数引起的,因此当您尝试使用 Integer.parseInt(betTextFieldAmount); 转换它时会抛出异常.

您应该使用 try...catch 语句包围 parseInt 调用,并在发生这种情况时向用户显示适当的错误消息。

更好的是,首先尝试通过限制可以在字段中输入哪些字符来防止用户输入非数字值,并添加空检查以捕获字段中未输入值的情况。

关于java - 无法解析 Integer.ParseInt 抛出的 java.lang.NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31965164/

相关文章:

JavaScript 初学者 : ParseInt() misunderstanding

user-interface - Google 使用什么 UI 框架来构建 Gtalk?

java - 表单在 Apache Wicket 中分为选项卡

java - 我无法从禁用文本中获取文本值

java - 为什么 Jericho 解析器无法解析这段 HTML 代码?

python - 从 iPython shell 运行时 matplotlib mpl_connect 中的事件滞后

java - 围绕基于命令行的程序编写 gui 包装器

java - 不知道为什么这段代码会抛出 NumberFormatException。 (有效输入)

javascript - 当计时器为零时如何显示 div?

java - Oracle Hibernate 时间戳/日期映射问题