当前位置: 首页 > 知识库问答 >
问题:

Java异常在线程"AWT-EventQuue-0"java.lang.数字格式异常:对于输入字符串:"

邹玮
2023-03-14

我的代码过早地抛出了预期错误。我试图测试输入验证,但在验证发生之前我抛出了一个错误。当我尝试将代码恢复到以前的版本时,它会抛出与我现在处理的问题之前相同的错误。

输入的示例

下面的代码是第137行——明显的罪犯

int playerGuess = Integer.parseInt(playerGuessField.getText().trim());
    private void guessButtonClicked(){
        Validators validGuess = new Validators(this);
        int guessesRemaining = Integer.parseInt(guessesRemainingField.getText());
        int playerGuess = Integer.parseInt(playerGuessField.getText().trim());//error is thrown here
        
        if(validGuess.isValidGuess(playerGuessField, "guess")){
            //more code here but not necessarily relevant
        }
    }
    //basic structure of my validators
    public boolean isValidGuess(JTextComponent c, String fieldName){
        try {
            int validGuess = Integer.parseInt(c.getText());
            if(validGuess >= 1 && validGuess <= 10){
                return true;
            } else {
                showErrorDialog("Your " + fieldName + " must be between 1 & 10");
                c.requestFocusInWindow();
                return false;
            }
        } catch (NumberFormatException e) {
            showErrorDialog("Your " + fieldName + " must be between 1 & 10");
            c.requestFocusInWindow();
            return false;
        }
    }


这是一个例外。

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "asdf"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
        at java.base/java.lang.Integer.parseInt(Integer.java:660)
        at java.base/java.lang.Integer.parseInt(Integer.java:778)
        at GameGUI.guessButtonClicked(GameGUI.java:137)
        at GameGUI.lambda$0(GameGUI.java:94)
        at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
        at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
        at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
        at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
        at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
        at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617)
        at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
        at java.desktop/java.awt.Component.processEvent(Component.java:6382)
        at java.desktop/java.awt.Container.processEvent(Container.java:2264)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
        at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
        at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

共有1个答案

岳君之
2023-03-14

底线:你需要验证你的输入。

为此,我可以想出几个选择:

Scanner scanner = new Scanner(playerGuessField.getText().trim());
if (scanner.hasNextInt()) {
    int playerGuess =  scanner.nextInt();
} else {
    // handle the error
}
int playerGuess = 0;
try {
    playerGuess = Integer.parseInt(playerGuessField.getText().trim());
} catch (NumberFormatException nfe) {
    // handle the exception
}

为此,您需要创建一个输入验证程序

public class MyInputVerifier extends InputVerifier {
    @Override
    public boolean verify(JComponent input) {
        String text = ((JTextField) input).getText();
        try {
            BigDecimal value = new BigDecimal(text);
            return (value.scale() <= Math.abs(4));
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

然后,需要将其添加到字段中

field.setInputVerifier(new MyInputVerifier());

最后,您需要触发验证。这通常是在表单呈现时完成的。我的例子是使用ActionListener

btn.addActionListener(e -> {
    if (!field.getInputVerifier().verify(field)) {
        System.out.println("Not valid. Clear!");
        field.setText("");
    }
});
public class InputVerifierDemo {
    private static JLabel errorMsg = new JLabel("Invalid input");
    private static JFrame frame = new JFrame();
    public static void main(String[] args) {
        JTextField field = new JTextField(10);
//      field.setInputVerifier(new MyInputVerifier());
        JButton btn = new JButton("Click Me!");
//      btn.addActionListener(e -> {
//          if (!field.getInputVerifier().verify(field)) {
//              System.out.println("Not valid. Clear!");
//              field.setText("");
//          }
//      });
        JPanel panel = new JPanel();

        errorMsg.setForeground(Color.RED);
        errorMsg.setVisible(false);
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5, 0, 0, 5);
        c.gridx = 1;
        c.gridy = 0;
        c.anchor = GridBagConstraints.SOUTH;
        panel.add(errorMsg, c);

        c.gridx = 1;
        c.gridy = 1;
        c.anchor = GridBagConstraints.CENTER;
        panel.add(field, c);

        field.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                validateInput(field);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                validateInput(field);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            } // Not needed for plain-text fields
        });

        frame.getContentPane().add(panel);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private static void validateInput(JTextField field) {
        String text = field.getText();
        String pattern = "\\d+";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(text);
        if (m.matches()) {
            errorMsg.setVisible(false);
        } else {
            errorMsg.setVisible(true);
        }
    }

    private static class MyInputVerifier extends InputVerifier {
        @Override
        public boolean verify(JComponent input) {
            String text = ((JTextField) input).getText();
            try {
                BigDecimal value = new BigDecimal(text);
                return (value.scale() <= Math.abs(4));
            } catch (NumberFormatException e) {
                return false;
            }
        }
    }
}

上面的代码包含输入验证器和文档侦听器解决方案,因此您可以同时使用这两种解决方案。我个人更喜欢文档监听器,因为它更具交互性。我宁愿在输入字段时验证字段,而不是在填写完所有字段并执行提交表单的操作之后。但是,这都是个人喜好的问题。

我把选项#1归功于程序员。

 类似资料: