0

I am a beginner programmer (first post here!) and I cannot figure out how to create an error message with a "do while" loop. If given input that's not in the alphabet it is supposed to display the error message until given an input with only letters and then move on the to rest of the program. My code seems to loop forever even if given correct input. Any suggestions are greatly appreciated! :)

do {
    input = JOptionPane.showInputDialog("What is your name?");  
    if (input.contains("[a-zA-Z]"))  
        name = input;
    else
         System.out.println("Please enter a valid name containing: ‘a-z’ or ‘A-Z’ lower or upper case");
} while (!input.contains("[a-zA-Z]")); 
0

5 Answers 5

1

You can show user an error popup so he knows something is wrong with his/her input.

In else statement where you are Printing message just do

JOptionPane.showMessageDialog(frame,
"Please enter a valid name containing: ‘a-z’ or ‘A-Z’ lower or upper case.",
"Input error",
JOptionPane.ERROR_MESSAGE);

return;

For more information

Sign up to request clarification or add additional context in comments.

Comments

0

You use the wrong method to validate your regex. .contains() expects a char sequence. That is not what you want. You should instead use the .matches() method.

String input = "";
do{
input = JOptionPane.showInputDialog("What is your name?");  
    if (input.matches("[a-zA-Z]+"))  // Replacing the contains()
        name = input;
      else
         System.out.println
    ("Please enter a valid name containing: ‘a-z’ or ‘A-Z’ lower or upper case");
}while (!input.matches("[a-zA-Z]+")); //Once again replacing contains()

Comments

0

input.contains("[a-zA-Z]") checks if input contains the text [a-zA-Z].

You can use the matches method.

input.matches("[a-zA-z]+")

Also, you should use [a-zA-z]+ as the pattern, since you are trying to match one or more alphabets.

Comments

0

The problem is related to how you check for the given input.

 String input;
        String name = "";
        do {
            input = JOptionPane.showInputDialog("What is your name?");
            if (input.matches("[a-zA-Z]+")) {
                name = input;
            } else {
                System.out.println("Please enter a valid name containing: ‘a-z’ or ‘A-Z’ lower or upper case");
            }
        } while (!input.matches("[a-zA-Z]+"));

using the matches function of string can help you achieve what you want

Comments

0

String.contains() method matches a set of character sequence. What you've specified is a regular expression, hence, you should be using String.matches() or the Pattern class to perform the check.

It should be something like this using the Pattern class.

Pattern p = Pattern.compile("[a-zA-Z]+");
Matcher m = p.matcher(input);
if(m.matches){
 //Do something
}

Refer to this documentation for more details

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.