0

I am working on a project that involves asking a user for their zip code. Using the zip code provided the program should loop through a .csv file to determine what city they live in. I can read the information in the .csv file but I have no idea how to loop through it to find a specific piece of information.

import java.util.Scanner;
import java.io.*;

public class DetermineCity {

public static void main(String[] args) throws IOException {

    String zip = "99820,AK,ANGOON";

    Scanner keyboard = new Scanner(System.in);

    System.out.println("enter then name of a file");
    String filename = keyboard.nextLine();

    File file = new File(filename);
    Scanner inputFile = new Scanner(file);
    String line = inputFile.nextLine();

    System.out.println("The first line in the file is ");
    System.out.println(line);
    inputFile.close();
  }
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283
Jsphty
  • 19
  • 1
  • 5
  • What have you done so far ??? – ThisaruG Apr 27 '15 at 03:35
  • I posted my code, im just playing around with the code to make sure i could access the information in the file. – Jsphty Apr 27 '15 at 03:37
  • I think what you're looking for is [here](http://stackoverflow.com/questions/14274259/java-read-csv-with-scanner). – Ray Hylock Apr 27 '15 at 03:43
  • If you want to continue with `inputFile.nextLine()`, then you can always split it on `,` and grab the appropriate index for the city if the zip code matches. – Ray Hylock Apr 27 '15 at 03:47
  • Yeah i see that from the link you posted. I tried splitting it but i cant seem to get it to work. How would i need to edit the code in order for it to split? – Jsphty Apr 27 '15 at 03:52
  • Its not really about what i want to continue with, its more the only thing i know at this point in time. Would you suggest a different approach ? – Jsphty Apr 27 '15 at 03:54

2 Answers2

1

Use Scanner.hasNext() method to loop

String Details="";
int ZipCodeIndex=0;
String ZipCode = "10230"
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext()){
     String x=inputFile.nextLine();
     String[] arr=x.split(",");
     if(ZipCode.equals(arr[ZipCodeIndex]))
     {
       Details=x;
       break;
     }
}
prasadmadanayake
  • 1,395
  • 13
  • 22
0

This assumes the format of your file is of the form "2301,Suburb, City, Country"

  1. the .nextLine() function returns a String of the next line, however return null if their isn't a line. So using a while loop you can go through your file and store each line in a string.
  2. Then using .split() method you would break this string using a delimiter ",". This would be stored in an array.
  3. Then compare the user zip code with the first value of the array. If they match then you have an array with the city and other information. Then a break statement as you have found the city.

    String suburb;
    String[] lineArray;
    String line = null;
    while((line = inputFile.nextLine()) != null){
        lineArray[] = line.split(",");
        if(lineArray[0] == zipCodeString){ 
            suburb = lineArray[1];
            break;
        }
    }
    
Wally
  • 47
  • 7