0

ZIPCODE, CITY, STATE, LATITUDE, LONGITUDE
ZIPCODE, CITY, STATE, LATITUDE, LONGITUDE

I'm trying to make this able to open a text file with addresses formatted as such, Create a loop that instantiates a new ZipCode object with the five perameters in order, and then adds that object to ArrayList myZips.

I have a feeling that at least my delimiters are wrong.

public void readZipCodeData(String filename){

Scanner inFS = null; 
FileInputStream fileByteStream = null;

try{
    // open the File and set delimiters
    fileByteStream = new FileInputStream(filename);
    inFS = new Scanner(fileByteStream);
     inFS.useDelimiter(", *");

    // continue while there is more data to read
    while(inFS.hasNext()) {

        // read five data elements
        int zipCode  = inFS.nextInt();
        String city  = inFS.next();
        String state = inFS.next();
        double latitude  = inFS.nextDouble();
        double longitude = inFS.nextDouble();
        ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
        myZips.add(z1);
    }
    fileByteStream.close();

    // Could not find file
    }catch(FileNotFoundException error1) {
        System.out.println("Failed to read the data file: " + filename);
    // error while reading the file                      
    }catch(IOException error2) {
        System.out.println("Oops! Error related to: " + filename);
}        

}

Everytime I try running it as is it gives me a
java.util.InputMismatchException: null (in java.util.Scanner) error on the double longitude line. Any ideas?

David
  • 89
  • 9

2 Answers2

0

I'm not familiar with Scanner for input, but rather BufferedReader. I find it simple to use, and I hope this solution works for you:

Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(filename, charset)) {
    String line = null;
    while ((line = reader.readLine()) != null) {

        // THIS IS THE CODE FOR EVERY LINE
        String[] data = line.split(", ");
        int zipCode = Integer.parseInt(data[0]);
        String city = data[1];
        String state = data[2];
        double latitude = Double.parseDouble(data[3]);
        double longitude = Double.parseDouble(data[4]);
        ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
        myZips.add(z1);

    }
} catch (IOException x) {
    System.err.format("IOException: %s%n", x);
} catch (NumberFormatException x) {
    System.err.format("NumberFormatException: %s%n", x);
}

In this example, I read a whole line with BufferedReader.readLine(), and manually parse it using String.split() and Integer.parseInt() / Double.parseDouble(). It's more intuitive, and it works!

See working example here.


Because of the comment below, I guess I cannot suggest the answer above. However, there are two potential problems I see:

  • You might have not put in a double in the input file. Simple, honest mistake.
  • Or, by this SO answer, your locale may be set wrong. In some places, they use a , for decimal point instead of . Try switching these around.
Community
  • 1
  • 1
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
0

Try something like this. Instead of parsing one field at a time, grab the whole line, turn it into an array of String using your delimiter , then parse to int/double. Scanner.nextLine() grabs the whole line for you.

try{
    // open the File and set delimiters
    fileByteStream = new FileInputStream(filename);
    inFS = new Scanner(fileByteStream);


        // read five data elements
            String[] data = inFS.nextLine().split(", ");
            int zipCode = Integer.parseInt(data[0]);
            String city = data[1];
            String state = data[2];
            double latitude = Double.parseDouble(data[3]);
            double longitude = Double.parseDouble(data[4]);
        ZipCode z1 = new ZipCode(zipCode, city, state, latitude, longitude);
        myZips.add(z1);
gonzo
  • 2,103
  • 1
  • 15
  • 27