Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Parsing CSV file

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parsing CSV file

    Ok, so I have a basic understanding of how to parse a csv file. My CSV basically looks like this:

    date,name,weeks,cost
    2012,steve,10,4000
    2012,mike,8,2000
    2011,cindy,15,5500
    I am using this code to read the csv file:

    public void readCSV() {
            try {
            BufferedReader CSVFile = new BufferedReader(new FileReader("list.csv"));
     
            // Read first line.
            // The while checks to see if the data is null. If 
            // it is, we've hit the end of the file. If not, 
            // process the data.
            String dataRow = CSVFile.readLine();
     
            while (dataRow != null){
                String[] dataArray = dataRow.split(",");
     
                for (String item:dataArray) { 
                    System.out.print(item + "\t");
                }
                System.out.println();
                dataRow = CSVFile.readLine();
            }
     
            CSVFile.close();
            System.out.println();
            }
     
            catch(FileNotFoundException e) {
                e.printStackTrace();
            }
     
            catch(IOException e) {
                e.printStackTrace();
            }
        }

    Okay, so the code reads line by line, if there is data on the line, it adds it to the string array, then prints out that string array.

    Now here's my question:
    Would it be possible to add each individual entry of the csv file to a place in an array list. For example, could I create some arraylist named "list" and add each piece of data to it one by one? So that "date" would be at index 0, "name" would be at index 1, etc.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Parsing CSV file

    Would it be possible to add each individual entry of the csv file to a place in an array list. For example, could I create some arraylist named "list" and add each piece of data to it one by one? So that "date" would be at index 0, "name" would be at index 1, etc.
    Sure, but java is object oriented so why not use the power of classes - create a class that represents the specific data, as you parse each line you create a new instance of this class with the data of the line, then add that instance to a List.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Parsing CSV file

    Quote Originally Posted by copeg View Post
    Sure, but java is object oriented so why not use the power of classes - create a class that represents the specific data, as you parse each line you create a new instance of this class with the data of the line, then add that instance to a List.
    That is my goal in the end of things. I have already created classes to represent the data I want to handle. Right now I more or less want to understand more about how I am parsing the file. Getting all the data into an arraylist and then iterating through and creating the objects seemed easier in my head, but I guess directly creating the objects makes sense too.

    Correct me if I'm wrong, but I am parsing line by line, and it is adding that WHOLE line to the string array. What I want to do is parse a line, take "2012", make it a "Date" object, then take "Steve" and make it a "Name" object, and so on.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Parsing CSV file

    Let me get this straight, you have been suggested the correct way to do this but instead you want to do it the incorrect way first???

    Read a line of data
    Split the data
    Create a new instance of a class and pass data as parameters to constructor
    Add new object to List

    It's not that hard.
    Improving the world one idiot at a time!

Similar Threads

  1. csv or text file to Multiple csv or text file
    By robert1994 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 19th, 2013, 01:35 PM
  2. read csv file in java
    By kishore1981 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2012, 07:52 AM
  3. Trying to read a CSV file !
    By Cruz182 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 2nd, 2011, 01:30 PM
  4. Printing name tags from a csv file
    By infoman in forum Java Theory & Questions
    Replies: 8
    Last Post: January 25th, 2011, 05:20 AM
  5. Averages, Highest & using a .csv file ! ? ! ?
    By thebigtimeGNAR in forum Java Theory & Questions
    Replies: 3
    Last Post: April 14th, 2010, 11:35 AM