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:

Use access modifiers with postal 1 and postal2 and then use get() and set() methods for these two strings so that you can refer to them at any place using the get() methods.

Use access modifiers with postal 1 and postal2 and then use get() and set() methods for these two strings so that you can refer to them at any place using the get() methods.

that's a bit redundant.
he already has those two variables outside of his while loop, simply because they are declared outside of his loop.

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:

well i see no reason why you cant use the variables outside the while loop, as it is still in the scope:

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;
      }
}
System.out.println(postal1+" "+postal2);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.