Question

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. Also, how do I implement this code in the actionPerformed method?

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:

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))
            { 

            }
        }
    }
}
Was it helpful?

Solution

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())). If the parse doesn't work because something than a number is not entered, it will throw a 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

public boolean isNumber(String input){
    try {
        Integer.parseInt(input);
        return true;
    } catch (NumberFormatException ex){
        return false;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top