1
Pattern p = Pattern.compile("^[0-9]{0,3}$");
Matcher m = p.matcher(in);
if (m.find()) {
    JOptionPane.showMessageDialog(null, " 4 integers please");
}

It is a button that adds numbers

Trying to create exception that limits amount of numbers in dialog , it detects if the number is within the limit but does not stop the program.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110

3 Answers3

0

I do not know the context of this code, but it does not call for a custom exception. Just use a loop to display the dialog if the user enters an invalid input:

Pattern p = Pattern.compile("^[0-9]{0,3}$");
Matcher m = p.matcher(in);
while (!m.find()) {
    in = JOptionPane.showInputDialog(null, "Please only enter four integers:");
    m = p.matcher(in);
}
// ...

Also, you want to change if(m.find()) to if(!m.find()) otherwise the "Please only enter four integers:" dialog will only show when the user enters the correct number of integers.


If you must use exceptions, just create a class that extends the Exception class:

public class MyException extends Exception {

    public MyException(int amount) {
        super("Only " + amount + " integers are allowed");
    }

}

And implement it in your if statement:

if (!m.find()) {
    throw new MyException(4);
}
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
  • I see where you are coming from , I have a try and catch block that does not accept letters , the code you provided still accepts the numbers. I guess a loop is the best way . Any ideas –  Nov 16 '20 at 20:40
  • or I could try != 4 –  Nov 16 '20 at 20:50
0

You've really provided less than needed code..

But here is a way to fire an event when the characters input in your TextArea exceed 3.

Lets say your TextArea is named txtArea.

    txtArea.textProperty().addListener((observable, oldValue, newValue) -> {
        if(newValue.length()>3){
            JOptionPane.showMessageDialog(null, " 4 integers please");
            //Do whatever you need after the Alert is shown
            txtArea.setText(oldValue);
        }
    });
Zozinski
  • 73
  • 8
0

You can simply use in.matches("\\d{4}") as the condition and add to the textarea only if this condition is true.

import javax.swing.JOptionPane;

public class Main {
    public static void main(String[] args) {
        String in = JOptionPane.showInputDialog("Number: ");
        if (in.matches("\\d{4}")) {
            // ...Code to add the value to the textarea
        } else {
            JOptionPane.showMessageDialog(null, "Only 4 digits please.");
        }
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • This makes sense , but the it does not stop the numbers adding to the text area –  Nov 16 '20 at 21:06
  • the message dialog does pop up –  Nov 16 '20 at 21:06
  • It gives dispatch errors –  Nov 16 '20 at 21:10
  • @Java1245 - I've updated the answer to meet this requirement. – Arvind Kumar Avinash Nov 16 '20 at 21:12
  • I see , but this allows the numbers to the text area and it now does not show the message –  Nov 16 '20 at 21:17
  • @Java1245 - If you attempt to enter an integer which is not of 4 digits, this code will show the error message and the code to add to the text area will not be executed. On the other hand, if you enter an integer which is of 4 digits, the code to add to the text area will not be executed and the error message will not be displayed. Do you have a different requirement? – Arvind Kumar Avinash Nov 16 '20 at 21:21
  • Was an accident! , my bad –  Nov 18 '20 at 11:02