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
A1A1A1
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");
}
}
You can reduce a lot of if-else
statements and reduce the lines of your code.
The following code have same logic as yours and it's checking for the postal code as you want it to:
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:");
char charPC[] = kb.nextLine().toCharArray();
boolean valid = false;
if (charPC.length == 7) {
if (Character.isLetter(charPC[0]) && Character.isLetter(charPC[2]) && Character.isLetter(charPC[5])
&& Character.isDigit(charPC[4]) && Character.isDigit(charPC[1]) && Character.isDigit(charPC[6])
&& Character.isWhitespace(charPC[3])) {
valid = true;
}
}
else if (charPC.length == 6) {
if (Character.isLetter(charPC[0]) && Character.isLetter(charPC[2]) && Character.isLetter(charPC[4])
&& Character.isDigit(charPC[1]) && Character.isDigit(charPC[3]) && Character.isDigit(charPC[5])) {
valid = true;
}
}
if (valid)
System.out.println("\nValid");
else
System.out.println("\nInvalid");
System.out.println("Program ending due to end-of-file");
}
}