title: How to Validate Canadian postal Code in Java? Checking for 2 types of input? ex: accepting A1A1A1 and A1A 1A1
body: <p>I am trying to make a program that allows the user to input 2 different formats for a postal code (A1A1A1 or A1A  1A1) and I cannot for the life of me get it to work :/
  My thought process was identifying both formats first and then using an if statement to check for the identified formats and then decide if its valid or not.
  But I keep getting invalid when I try to enter the format with space in them (A1A 1A1).
  so far I have this </p>
  
  <pre><code>import java.util.Scanner;
  import java.util.regex.Pattern;
  
  public class ValidatingPostcodes {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a Postcode:");
      String Pc1 = "[A-Z][0-9][A-Z][0-9][A-Z][0-9]";
      String Pc2 = "[A-Z][0-9][A-Z][ ][0-9][A-Z][0-9]";
          while (sc.hasNext())
          { if (sc.hasNext (Pc1))
              System.out.println ("Valid Postal Code");
              else if (sc.hasNext (Pc2))
              System.out.println ("Valid Postal Code");
              else System.out.println("Invalid Postal Code");
              sc.next();
          }
      }
    }
  </code></pre>