简体   繁体   English

NumberFormatException方法

[英]NumberFormatException method

Hello everyone, I'd like to ask someone to help me. 大家好,我想请人帮我。 I want to write a method in the same class to use NumberFormatException that takes the value of a JTextField and checks if it is number and prints. 我想在同一类中编写一个方法以使用NumberFormatException ,该方法采用JTextField的值并检查其是否为数字并进行打印。 Also, how do I implement this code in the actionPerformed method? 另外,如何在actionPerformed方法中实现此代码?

This the main method 这个主要方法

public class main {
    public static void main(String args[]){
        JavaApplication19 is = new JavaApplication19("title");
        is.setVisible(true);
    }
}

This the GUI class: 这个GUI类:

public class JavaApplication19 extends JFrame{
    private final JButton button;
    private final JTextField text;
    private final JLabel lable;



    /**
     * @param args the command line arguments
     */ 
    public JavaApplication19(String title){
        setSize(500, 200);
        setTitle(title);
        setDefaultCloseOperation(JavaApplication19.EXIT_ON_CLOSE);

        button = new JButton("enter only number or i will kill you");
        text = new JTextField();
        lable = new JLabel("numbers only");
        JPanel rbPanel = new JPanel(new GridLayout(2,2));
        rbPanel.add(button);
        rbPanel.add(text);
        rbPanel.add(lable);

        Container pane = getContentPane();
        pane.add(rbPanel);

        button.addActionListener(new ButtonWatcher());

        private class ButtonWatcher implements ActionListener{

        public void actionPerformed(ActionEvent a){
            Object buttonPressed=a.getSource();
            if(buttonPressed.equals(button))
            { 

            }
        }
    }
}
if(buttonPressed.equals(button))
{
    try {
        //  try something
    }
    catch (NumberFormatException ex) {
        // do something
    }
}

The // try something should be the code where you're getting the input text and parsing (ex Integer.parseInt(textField.getText()) ). // try something应该是获取输入文本并进行解析的代码(例如Integer.parseInt(textField.getText()) )。 If the parse doesn't work because something than a number is not entered, it will throw a NumberFormatException 如果解析由于未输入数字而不起作用,则将引发NumberFormatException

See Exceptions tutorial if you need more information on how to use exceptions 如果您需要有关如何使用异常的更多信息,请参见异常教程。

Edit: Method 编辑:方法

Something simple like this would work 像这样简单的事情会起作用

public int parseInput(String input) throws NumberFormatException {
    return Integer.parseInt(input); 
}

Or something like this if you want to catch the exception 或者类似这样的东西,如果您想捕获异常

public static int parseInput(String input) {
    int number = 0;
    try {
        number = Integer.parseInt(input); 
    } catch (NumberFormatException ex) {
        someLabel.setText("Must be a number");
        return -1;  // return 0 
    }
}

Then in your actionPerformed you can do something like this 然后在您执行的动作中,您可以执行以下操作

if(buttonPressed.equals(button))
{
    int n;
    if (parseInput(textField.getText()) != -1){
        n = parseInput(textField.getText());
        // do something with n
    }
}

Edit: boolean Method 编辑:布尔方法

public boolean isNumber(String input){
    for (char c : input.toCharArray()){
        if (!Character.isDigit(c))
            return false;
    }
    return true;
}

Usage 用法

if(buttonPressed.equals(button))
{
    if (isNumber(textField.getText()){
        // do something
    }
}

Edit: or to catch exception 编辑:或catch异常

public boolean isNumber(String input){
    try {
        Integer.parseInt(input);
        return true;
    } catch (NumberFormatException ex){
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM