Arima Arima - 1 month ago 8
Java Question

Java validating postal code

I'm trying to write a program that will validate a Canadian postal code with the format A1A1A1 or A1A 1A1. I'm having problems getting my code to recognize the white space in the second format.

When I run it and try the second format, it will say invalid twice even though it's a legitimate postal code.

Any ideas?

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");
}
}

Answer

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");
   }
}    
Comments