1

I have a CSV file of US population data for every county in the US. I need to get each population from the 8th column of the file. I'm using a fileReader() and bufferedStream() and not sure how to use the split method to accomplish this. I know this isn't much information but I know that I'll be using my args[0] as the destination in my class.

I'm at a loss to where to being to be honest.

import java.io.FileReader;

public class Main {

    public static void main(String[] args) {
      BufferedReader() buff = new BufferedReader(new FileReader(args[0]));
      String 
    }
    try {

    }
}

The output should be an integer of the total US population. Any help with pointing me in the right direction would be great.

GMc
  • 1,764
  • 1
  • 8
  • 26
javaJunk
  • 21
  • 1
  • 4
    What you are trying to do here is to reimplement a csv parser. By experience i can tell you that its a painful road. You would have to deal with escaping quotes, escaping commas, dealing with charsets etc. Instead try using http://super-csv.github.io/super-csv/index.html or some other similar library. These libs are tried and tested and speed up your dev time. Here's some sample code : https://super-csv.github.io/super-csv/examples_reading.html . – Arpan Kanthal Aug 05 '19 at 21:44
  • This has been asked a million times before. https://stackoverflow.com/a/844185/1044799 – awm Aug 05 '19 at 21:47
  • Assuming you have copied your "attempt" correctly, you cannot have a try outside of a method. Try needs to surround a block of code that might "throw" an error (Exception). Thus it needs to be inside (not outside) your main method. – GMc Aug 05 '19 at 21:55
  • 2
    Also, I completely agree about using a library for parsing CSV, but I am guessing your *homework assignment* is to use split. Did you try searching for "how to split a string on a character in java"? If so, try working that into your assignment and when you get stuck, ask a follow up question. Alternatively, if you are free to choose your own method to process the csv file, follow the excellent advice here and use a library. Again, when you get stuck with the library approach, ask a follow up question. – GMc Aug 05 '19 at 21:58

1 Answers1

3

Don't reinvent the wheel, don't parse CSV yourself: use a library. Even such a simple format as CSV has nuances: fields can be escaped with quotes or unescaped, the file can have or have not a header and so on. Besides that you have to test and maintain the code you've wrote. So writing less code and reusing libraries is good.

There are a plenty of libraries for CSV in Java:

IMHO, the first two are the most popular.

Here is an example for Apache Commons CSV:

final Reader in = new FileReader("counties.csv");
final Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);

for (final CSVRecord record : records) { // Simply iterate over the records via foreach loop. All the parsing is handler for you
    String populationString = record.get(7); // Indexes are zero-based
    String populationString = record.get("population"); // Or, if your file has headers, you can just use them

    … // Do whatever you want with the population
}

Look how easy it is! And it will be similar with other parsers.

madhead
  • 31,729
  • 16
  • 153
  • 201