0

I have a GUI design that has a number of fields that the user enters. I'm using a try-catch to deal with the exceptions that the user incorrectly enters. I was successful when the user enters a String into a number field (My id field) but I'm frustrated here trying to use an exception to handle when a user enters an integer into a text/String field. Here is my code to give you an idea of what I did for the exception i did successfully. Thank you.

try {

    String taskID = txtId.getText();
    int id = Integer.parseInt(taskID);

    String taskName = txtName.getText();

    String taskDesc = txtDesc.getText();

    boolean isCompleted = chkComp.isSelected();

    int taskPriority = comboPriority.getSelectedIndex();

    Task newTask = new Task(id, taskName, taskDesc, isCompleted, taskPriority);

    tasks.add(newTask);
    taskMod.addElement(newTask);

} catch (NumberFormatException e) {
    JOptionPane.showMessageDialog(null, "Error: You must enter an integer");

}
3
  • Hi, thank you for the reply, But what time op exception will that be? Like this was a "NumberFormatException"? Sorry i'm new to learning this. Commented May 4, 2014 at 20:53
  • Well I'm a little confused here. Is your goal to get the program to prompt again? Commented May 4, 2014 at 22:00
  • Yes, my goal is to prompt the user "You must enter A String in Name not an integer"..sorry if i'm not making much sense. My Goal is to get it to prompt for either error Commented May 4, 2014 at 22:08

1 Answer 1

1

I have another option for you, You can validate the input in real time, with a DocumentFilter. With this, you validate each character input. If the character does not match what you want, it won't be allowed to be entered.

Here is for numbers only

private JTextField createNumberField() {
    JTextField field = new JTextField(20);
    ((AbstractDocument) field.getDocument()).setDocumentFilter(new DocumentFilter() {
        @Override
        public void insertString(FilterBypass fb, int off, String str, AttributeSet attr)
                throws BadLocationException {
            fb.insertString(off, str.replaceAll("\\D", ""), attr);  // remove non-digits
        }

        @Override
        public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr)
                throws BadLocationException {
            fb.replace(off, len, str.replaceAll("\\D", ""), attr);  // remove non-digits
        }
    });
    return field; 
}

Here is for name (letters, - , and spaces) are allowed

private JTextField createNameField() {
    JTextField field = new JTextField(20);
    ((AbstractDocument) field.getDocument()).setDocumentFilter(new DocumentFilter() {
        @Override
        public void insertString(DocumentFilter.FilterBypass fb, int off, String str, AttributeSet attr)
                throws BadLocationException {
            fb.insertString(off, str.replaceAll("[^a-zA-Z\\s\\-]", ""), attr);  // remove non-digits
        }

        @Override
        public void replace(DocumentFilter.FilterBypass fb, int off, int len, String str, AttributeSet attr)
                throws BadLocationException {
            fb.replace(off, len, str.replaceAll("[^a-zA-Z\\s\\-]", ""), attr);  // remove non-digits
        }
    });
    return field;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.