Results 1 to 18 of 18
  1. #1
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default Reading csv file into 2D array - HELP!!!

    Hi,

    I'm trying to read a csv file consisting of 994 rows and 3 columns of entirely doubles into an array and then find the sum of each column. I have managed to write code to import the data as a string and populate a 2D array. The problem I'm having is then trying to convert these objects into 'real' data so that I can perform calculations on them. I've been trying to do this for 3 weeks now and can't seem to get anywhere with it. I have only been programming for 3 weeks as part of a PhD in Engineering and so woyuld be VERY grateful of any help. I have included my code so far below.

    Many thanks is advance

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.StringTokenizer;
    
    
    public class csvimport5 {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		
    		String [][] data = new String [994][3]; 	
    		File file = new File("datum.csv");
    		int row = 0;
    		int col = 0;
    		BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
    		String line = null;
    		
    	 
    		//read each line of text file
    		while((line = bufRdr.readLine()) != null && row < 994)
    		{	
    		StringTokenizer st = new StringTokenizer(line,",");
    		while (st.hasMoreTokens())
    		{
    			//get next token and store it in the array
    			data[row][col] = st.nextToken();
    			col++;
    		}
    		col = 0;
    		row++;
    		}
    		String[]values = line.split(",");
    	    for(String readline : values){
    	    	double str_double = Double.parseDouble(readline);
    	    	
    	    	   	row++;{
    	    		System.out.println(str_double);}
    	    	   	
    }
    	System.out.println("value is "+data[993][2]);
    }
    	}

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    My suggestion is not to first store the data from the file in a String array and then convert it later. Instead, immediately convert it to Double.

    So, after the while loop you have an 2D array with doubles. Then build a double for loop to iterate through the rows and columns and store the sum of each collumn, e.g. in a new array with 3 elements.

    The code

    Java Code:
        String[]values = line.split(",");
        for(String readline : values){
            double str_double = Double.parseDouble(readline);
    	    	
    	row++;
    	System.out.println(str_double);   	   	
        }
    will definitely not work since line is already null by the time the program gets there.

    Good luck,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Ah ok that makes sense now. Thank you very much, really appreciated. Do you know what the code would look like in order to do that? Do I just put a double parse command in just before the st.nexttoken command?

    Thanks again

  4. #4
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Yep that's the idea. Something like

    Java Code:
    data[row][col] = Double.parseDouble(st.nextToken());
    would do it.

    Erik
    I'm new to Java but I like to help where ever I can. :)

  5. #5
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Ok I'll give that a go and let you know. Thanks again for your help

    Mike

  6. #6
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    You are welcome...
    I'm new to Java but I like to help where ever I can. :)

  7. #7
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    I've had a go at adding the double parse but I get an error saying "cannot convert double to String" and am not sure what this means or why my code is wrong. My new code is below. I've taken out the line.split command as I realised this wasn't needed with the tokenizer and it still works fine apart from the double parse. Many thanks

    Mike

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.StringTokenizer;
    
    
    public class csvimport5 {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		
    		String [][] data = new String [994][3]; 	
    		File file = new File("datum.csv");
    		int row = 0;
    		int col = 0;
    		BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
    		String line = null;
    		
    	 
    		//read each line of text file
    		while((line = bufRdr.readLine()) != null && row < data.length)
    		{	
    		StringTokenizer st = new StringTokenizer(line,",");
    		while (st.hasMoreTokens())
    		{
    			//get next token and store it in the array
    			data[row][col] = Double.parseDouble(st.nextToken());
    			col++;
    		}
    		col = 0;
    		row++;
    		}
    		
    	    System.out.println("value is "+data[993][2]);
    	
    }
    	
    	}

  8. #8
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    You haven't changed the array from a String array to a Double array. You are now converting the string to a double and then want to store it in an String array...
    I'm new to Java but I like to help where ever I can. :)

  9. #9
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    So, do I need to declare a new double array to place the converted doubles in? Thanks for this

    Mike

  10. #10
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    I'd say give it a try.
    I'm new to Java but I like to help where ever I can. :)

  11. #11
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    Hooray! Got it to work now! Just thought, I already had an array declared but a String array. So, just changed it to double and all working fine now. I can't even begin to tell you how relieved I am that it's now working. I've literally spent tens of hours trying to figure it out! Thank you very much for all your advice.

    Mike

  12. #12
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    I am glad that I could be of any help.
    I'm new to Java but I like to help where ever I can. :)

  13. #13
    mikeg is offline Member
    Join Date
    Nov 2010
    Posts
    25
    Rep Power
    0

    Default

    You certainly were very helpful. Put it this way, I've read numerous books and watched video tutorials but got nowhere. Thank you for giving up your time to help. Have managed to sum the columns aswell now so am very happy!

    Mike

  14. #14
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Once more, you are welcome. I like to help where ever I can. It's educational for me too...

    Do you know you can express your thankfulness by giving reputation? Just click on the REP button in one of my replies.

    Cheers,
    Erik

    BTW Don't hesitate to come back with more questions. There are lots of people here willing to help you.

    BTW2 Please mark this thread as SOLVED to let other people know it (there is a menu in your thread to change the state of your thread).
    I'm new to Java but I like to help where ever I can. :)

  15. #15
    dimiro is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    hi, newbie here...

    i have the same problem and i cannot understand what did you change to upload the csv file into a 2d array

    the code is like this


    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    //import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.StringTokenizer;
    
    
    public class readCSV {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		String  [][] data=new String [50][2];
    		File file=new File("test.csv");
    		
    		int row=0;
    		int col=0;
    		BufferedReader reader=new BufferedReader(new FileReader(file));
    		String line=null;
    		//String e=reader.readLine();
    		
    		while((line=reader.readLine())!=null && row<data.length){
    			StringTokenizer st=new StringTokenizer(line, "\t");
    			while(st.hasMoreTokens()){
    				data[col][row]=Float.parseFloat(st.nextToken());
    				
    				col++;
    			}
    			col=0;
    			row++;
    		}
    	}
    	
    	}

    and no matter what i try i get an error on the line

    Java Code:
    data[col][row]=Float.parseFloat(st.nextToken());
    i did as you said to change the string array into float array but then i get an java.lang.ArrayIndexOutOfBoundsException: 1

    can you please help me ? it seems like i must do small modification but i cannot find it..

    thank you in advance...

  16. #16
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Hi dimiro,

    Do you understand what the exception is telling you? It is quite descriptive to be honest.

    You could add some debug statements to print out the values of col and row.

    Good luck,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  17. #17
    dimiro is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    thank you so much!!

    although i was craving for one line of code that would show me the mistake, debugging helped me a lot (i did not use it so much)

    the mistake was that i had the array -> data [col][row] and the correct is -> data[row][col]

    not to myself: use debug :)

  18. #18
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    I'm glad I could be of help.

    Cheers,
    Erik
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. Help with reading from file into an array
    By Trad in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 12:16 PM
  2. Reading a txt file and then storing it in a 2d array
    By blkshp1990 in forum New To Java
    Replies: 2
    Last Post: 11-05-2009, 12:31 AM
  3. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:04 PM
  4. Reading from a file to make an array
    By Bomber_Will in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 08:19 AM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •