0

I am trying to validate a phone number input from a user(in my project). This is the method I created trying to validate it and also give them 3 tries to enter a valid format. However what ever I cannot get it to return the prompt that tells them to re enter the number it just runs it true or false. Where am I going wrong.

public static boolean getPhoneInput(String phone) {
int count =0;
String input,
inputString = phone;          
input = JOptionPane.showInputDialog( phone );
String pattern="(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}";               
Pattern p = Pattern.compile(pattern);       
Matcher m = p.matcher(inputString);  
     while ((phone != null && input.length() == 0) && (count <2)){
         input = JOptionPane.showInputDialog("No input was detected \n" + phone);
         count++;}

     if (count==2){
            JOptionPane.showMessageDialog(null, " Sorry due to errors your order cannot be processed.  GOODBYE.");
            System.exit(0);      {           

      }
    }
    return false;} 

I tried to use an example I found and modify it for my program. I am unsure how to get the Boolean type to return a message if they leave the field blank or in an invalid format.

I was able to get everything working like a charm within my code using the getPhoneInput() method, however is there a way to return something other than a true or false with a Boolean. I understand that is what it was built to do, but once I have the correct format i want the number written to a txt file. as it works now instead of logging a number with the users name it just tells me that their phone number matched the required format or that it did not with a true or false.

3
  • return a string instead of boolean, booleans only return true or false (1,0). Commented Apr 26, 2014 at 17:08
  • I would but where I am unsure how to use the pattern matching with a string I have only ever seen it used with Boolean and I am not 100% on how to do that. Commented Apr 26, 2014 at 17:12
  • if(getPhoneInput(phone)){ send msg ok }else{ send msg not ok }, you can also return a string containing the error an use it to display whatever you want. Commented Apr 26, 2014 at 17:14

1 Answer 1

1

I've not thoroughly tested this, but it should work for you:

public static boolean getPhoneInput(String displayText)
{
    int count = 0;
    String input;
    input = JOptionPane.showInputDialog(displayText);
    String pattern = "\\d-(\\d{3})-(\\d{3})-(\\d{4})";
    Pattern p = Pattern.compile(pattern);
    while ((displayText != null && input.length() == 0))
    {
        input = JOptionPane.showInputDialog("No input was detected \n" + displayText);
        count++;
        if (count == 2)
        {
            JOptionPane.showMessageDialog(null, " Sorry due to errors your order cannot be processed.  GOODBYE.");
            System.exit(0);
        }
    }
    count = 0;
    while (count <= 3)
    {
        count++;
        Matcher m = p.matcher(input);
        if (m.matches())
            return true;
        else
            input = JOptionPane.showInputDialog("Input in wrong format. \n" + displayText);
    }
    return false;
}

Changes Made:

  1. Moved if statement to inside the while loop, changed while condition, as if statement will already cause the loop to exit if count is 2.
  2. Changed your regex (moved dashes outside of parens, got rid of ?)
  3. Added an actual check to see if the string matched the pattern.
  4. Moved Matcher m = p.matcher(input); to end of method, otherwise an initial empty input will always return false.
  5. inputString was superfluous.
  6. You had an extra set of braces following the if that served no discernible purpose.
  7. Added extra validation step.

I'd also like to point out that you should probably not use this regex for any real world applications, as it only validates one, very specific, form of phone number.

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

2 Comments

Thank you for the over all improvements. It does accomplish the need for them to enter some input (no blank field) and returns the message till the count 2 is reached. Is there a way to make it do that though if the pattern matcher returns false? say they enter the format 1234-123-123 I want them to get a message that informs them that was the wrong format and give them 2 additional tries to complete it. I am not sure how to do that with Boolean.
Thank you ... that was much more simple that what i was expecting...and no I wasn't planing on using this for a real world app I know it would limit the form and types of numbers that could be enter. Once again thank you seeing it laid out it makes since... I was lost in the sauce though.

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.