Search code examples
javavalidationwhitespacepostal-code

Validation of Canadian postal code fails when whitespace inside


I'm trying to write a program that will validate a Canadian postal code.

Given the two formats

  1. A1A1A1
  2. A1A 1A1

Problem

I have problems getting my code to recognize the white space in the second format.

When I validate for the second format, it prints "Invalid" twice even though it's a legitimate postal code.

Code

public class validatePostalCodeTest {
  public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    System.out.println("Please enter postalcode:");
    while (kb.hasNext()){
        String posCode = kb.next();
        if (posCode.length() > 7) 
            System.out.println("\nInvalid");
        if (posCode.length() < 6) 
            System.out.println("\nInvalid");    
        if (posCode.length()== 7){
            boolean valid = true;
            for (int i = 0; i < posCode.length(); i++){
                char a = posCode.charAt(0);
                char b = posCode.charAt(2);
                char c = posCode.charAt(4);
                char d = posCode.charAt(1);
                char e = posCode.charAt(5);
                char f = posCode.charAt(6);
                char g = posCode.charAt(3);
                if(! Character.isLetter(a))
                    valid = false; 
                else if (! Character.isLetter(b))
                    valid = false;
                else if (! Character.isDigit(c))
                    valid = false;
                else if (! Character.isDigit(d))
                    valid = false;
                else if (! Character.isLetter(e))
                    valid = false;
                else if (! Character.isDigit(f))
                    valid = false;
                else if (! Character.isWhitespace(g))
                    valid = false;
                break;
            }
            if (valid) System.out.println("\nValid");
            else System.out.println("\nInvalid");
        }
        if (posCode.length()== 6){
            boolean valid = true;
            for (int i = 0; i < posCode.length(); i++){
                char a = posCode.charAt(0);
                char b = posCode.charAt(2);
                char c = posCode.charAt(4);
                char d = posCode.charAt(1);
                char e = posCode.charAt(3);
                char f = posCode.charAt(5);
                if(! Character.isLetter(a))
                    valid = false; 
                else if (! Character.isLetter(b))
                    valid = false;
                else if (! Character.isLetter(c))
                    valid = false;
                else if (! Character.isDigit(d))
                    valid = false;
                else if (! Character.isDigit(e))
                    valid = false;
                else if (! Character.isDigit(f))
                    valid = false;
                break;                    
            }
            if (valid) System.out.println("\nValid");
            else System.out.println("\nInvalid");
        }
        System.out.println("\nPlease enter a postalcode:");
    }
    System.out.println("Program ending due to end-of-file");
  }
}

Solution

  • You have used kb.next() which will read data with space separation, So when you will enter postal code as A1A 1A1 it will take it 2 times first one is A1A and second one is 1A1 and therefor you will get invalid output 2 times as it will take 2 time data with one space, so you need to use nextLine() it will help you to resolve your issue,

    Check below resolved answer

    public class validatePostalCodeTest 
    {
       public static void main (String[] args) throws java.lang.Exception
       {
    
          Scanner kb = new Scanner(System.in);
          System.out.println("Please enter postalcode:");
          String posCode = kb.nextLine();
    
           if (posCode.length() > 7) 
               System.out.println("\nInvalid");
           if (posCode.length() < 6) 
               System.out.println("\nInvalid");    
           if (posCode.length()== 7){
               boolean valid = true;
    
                char a = posCode.charAt(0);
                char b = posCode.charAt(2);
                char c = posCode.charAt(4);
                char d = posCode.charAt(1);
                char e = posCode.charAt(5);
                char f = posCode.charAt(6);
                char g = posCode.charAt(3);
                if(! Character.isLetter(a))
                    valid = false; 
                else if (! Character.isLetter(b))
                    valid = false;
                else if (! Character.isDigit(c))
                    valid = false;
                else if (! Character.isDigit(d))
                    valid = false;
                else if (! Character.isLetter(e))
                    valid = false;
                else if (! Character.isDigit(f))
                    valid = false;
                else if (! Character.isWhitespace(g))
                    valid = false;
    
               if (valid) System.out.println("\nValid");
               else System.out.println("\nInvalid");
           }
           if (posCode.length()== 6){
               boolean valid = true;
    
                char a = posCode.charAt(0);
                char b = posCode.charAt(2);
                char c = posCode.charAt(4);
                char d = posCode.charAt(1);
                char e = posCode.charAt(3);
                char f = posCode.charAt(5);
                if(! Character.isLetter(a))
                    valid = false; 
                else if (! Character.isLetter(b))
                    valid = false;
                else if (! Character.isLetter(c))
                    valid = false;
                else if (! Character.isDigit(d))
                    valid = false;
                else if (! Character.isDigit(e))
                    valid = false;
                else if (! Character.isDigit(f))
                    valid = false;
    
    
               if (valid) System.out.println("\nValid");
               else System.out.println("\nInvalid");
           }
           System.out.println("Program ending due to end-of-file");
       }
    }