Hello
I'm writing a little program to check if a postal code is valid or not and I'm using while loop and if statement
this is the code:
final String REGEX = "[A-Z&&[^DFIOQU]][0-9][A-Z&&[^DFIOQU]] [0-9][A-Z&&[^DFIOQU]][0-9]";
output.println ("Please Enter a Valid Postal Code: ");
String postal1;
String postal2;
boolean isCorrect = false;
while (!isCorrect)
{
postal1 = input.nextLine();
if (!(postal1.matches(REGEX)))
{
output.println ("Please Enter a Valid Postal Code: ");
output.println("Invalid Postal Code! ");
}
else
{
output.println("enter another ");
postal2 = input.nextLine();
isCorrect = true;
}
The problem is I need to use the variables postal1 and postal2 outside the while loop after this code but I'm not sure how I would do that!
btw this is the requirement for the first part of the program:
Please enter a valid postal code:
The entry has to be made on the same line as the prompt.
Your program should determine if the input is a valid postal code using the rules outlined above. If the input is not valid, the following message must be displayed until a valid sequence is entered.
Invalid postal code!
Please enter a valid postal code: