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: Importing data from .csv into array

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Importing data from .csv into array

    Hello,

    I'm trying to import a csv file into a 2D array and then find the average of each column. The csv file contains 3 columns each with 994 rows and consists entirely of doubles. I am very new to Java programming but have managed to write some code to read the file and populate an array. The problem I'm having is the data all seems to be in one column instead of three? I've done alot of research and can't figure out what is missing. My code so far is below. Any help would be greatly appreciated!

    Thank you

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.StringTokenizer;
     
     
    public class csvimport {
     
    /**
    * @param args
    * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
     
    double [][] data = new double [994][2]; 
    String line = null;	
    int row = 0;	
    int col = 0; 	
     
    File file = new File("data.csv");
    BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
     
    //read each line of text file
    while((line = bufRdr.readLine()) != null && row < 994)
    		{	
    StringTokenizer st = new StringTokenizer(line,",");
    while (st.hasMoreTokens())
    st.nextToken();
    col++;
    {
    col=0;
     
    	}
    {	
    String[] values = line.split(",");
     
    for (String str : values) {
     
    double str_double = Double.parseDouble(str);
    data[row][col]=str_double;
    System.out.println(str_double);
     
    	}
     
    }
       }
    }
    }
    Last edited by copeg; November 10th, 2010 at 10:02 AM. Reason: Code Tags


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

    Default Re: Importing data from .csv into array

    For future reference, please surround your code with the [highlight=java][/highlight] tags.

    Not sure why you are using both StringTokenizer and split, which do redundant things. I'd say settle on using one. Further, when assigning data[row][col]=str_double;, I never see where row is incremented to assign the correct value to the correct row.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing data from .csv into array

    Many thanks for your quick reply, I really appreciate it. If I'm honest, I've only been programming for a few weeks as part of my engineering phd and so am a little new to some of the language. I understand what you mean about populating the array during parse so will try and amend my code and see if I can get it to work. I'll post once I've had a go.

    Thanks again for your help

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Importing data from .csv into array

    OK so I'm completely stumped! I can now read the data in OK and populate a 2D array with all the data in the correct place. The problem I've now got is that all the elements in the array are strings as oppose to doubles and I can't seem to get them to convert? Any ideas? Is there a better way of reading the data in so that it comes in as doubles rather than strings? Any help greatly appreciated - have been trying to get my head round this for 3 weeks now!!

Similar Threads

  1. Importing excel data
    By supriya ramjee in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 20th, 2012, 09:39 AM
  2. Importing gif/pictures
    By javanerd in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2010, 11:58 PM
  3. importing class files :S
    By b3ard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2010, 03:45 AM
  4. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM
  5. importing packages..
    By chronoz13 in forum Java IDEs
    Replies: 4
    Last Post: November 23rd, 2009, 05:49 PM