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.