Search code examples
javaregexstackqueuepostal-code

Java - Zip Code Validator not working?


I'm having a few issues with my zip code validator which reads a text file with UK postcodes then returns true or false after validation.

Below is the section of code I'm having issues with particularly (ZipCodeValidator) which is public and should be declared in a file called ZipCodeValidator.java. And then (Zip) which cannot find symbol.

public class ZipCodeValidator {
    private static String regex = "^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$";
    private static Pattern pattern = Pattern.compile(regex);

    public boolean isValid(String zipCode) {
        Matcher matcher = pattern.matcher(zip);
        return matcher.matches();
    }
}

And below here is the entire program for reference. Any help is appreciated.

package postcodesort;

import java.util.*;
import java.util.Random;
import java.util.Queue;
import java.util.TreeSet;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.zip.ZipFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;




public class PostCodeSort 
{
    Queue<String> postcodeStack = new LinkedList<String>();

    public static void main(String[] args) throws IOException 
    {
        FileReader fileReader = null;
        ZipCodeValidator zipCodeValidator = new ZipCodeValidator();

        // Create the FileReader object
        try {
            fileReader = new FileReader("postcodes1.txt");
            BufferedReader br = new BufferedReader(fileReader);

            String str;
            while((str = br.readLine()) != null) 
            {
                if(zipCodeValidator.isValid(str)){
                    System.out.println(str + " is valid");
                }
                else{
                    System.out.println(str + " is not valid");
                }
            }
        }
        catch (IOException ex) 
        {
            // handle exception;
        }

        finally 
        {
            fileReader.close();
        }

    }
}

public class ZipCodeValidator {
    private static String regex = "^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$";
    private static Pattern pattern = Pattern.compile(regex);

    public boolean isValid(String zipCode) {
        Matcher matcher = pattern.matcher(zip);
        return matcher.matches();
    }
}

Solution

  • Possibly a copy+paste issue, but Matcher matcher = pattern.matcher(zip); doesn't match the method parameter zipCode. Do you have zip defined somewhere else, and possibly validating against that?

    It's when I added the read file code thats when the problems arose like the ones I specified above

    Make sure you clean up the String before you pass it in. To remove any leading or trailing whitespace characters use

    if(zipCodeValidator.isValid(str.strip())){
    

    lastly, your regex only matches upper case. Make sure you allow all cases by using

    str.strip().toUpperCase()
    

    or changing your Regex:

    private static Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);