Results 1 to 4 of 4
  1. #1
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default Reading A CSV file error.

    Hi guys. Hope you can help me with this problem I am having in reading a CSV file. the csv is horizontally aligned so the first value of each row is a text field and than there are numbers. Ultimately I want to be able to calculate pearson's correlation between each variable in my file. I have got a class which is responsible for reading in a csv file line by line. The output is a long string. What I want is to split this string into individual arrays and than convert them to doubles. Can you please help as I am stuck on the part of splitting the string.
    The code of my class is as follow:
    Java Code:
    package fileReader;
    import java.io.*;
    
    
    public class FileReader {
    
       public static void main(String args[])
      {
          try{
        // Open the file that is the first 
        // command line parameter
        FileInputStream fstream = new FileInputStream("D:\\faith20.csv");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
      
        //Read File Line By Line
        while ((strLine=br.readLine()) != null)   {
       
        	//Split the string using regex
        	
        	String [] strArray =strLine.split("\\|");
          System.out.println(strArray);
      }
       
    
        //Close the input stream
        in.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
      }
       
       
       
    }
    The output from this code is below which I do not understand what it means:
    [Ljava.lang.String;@6af62373
    [Ljava.lang.String;@459189e1
    [Ljava.lang.String;@55f33675
    [Ljava.lang.String;@527c6768
    [Ljava.lang.String;@65690726
    [Ljava.lang.String;@525483cd
    [Ljava.lang.String;@2a9931f5
    [Ljava.lang.String;@2f9ee1ac
    [Ljava.lang.String;@24c21495
    [Ljava.lang.String;@41d5550d
    [Ljava.lang.String;@1cc2ea3f
    [Ljava.lang.String;@40a0dcd9
    [Ljava.lang.String;@7f5f5897
    [Ljava.lang.String;@4cb162d5
    [Ljava.lang.String;@11cfb549
    [Ljava.lang.String;@5b86d4c1
    [Ljava.lang.String;@70f9f9d8
    [Ljava.lang.String;@2b820dda
    [Ljava.lang.String;@675b7986

    I know that if i remove the line where I am splitting the string than I will get all the lines read out to the console but that is no use to me.
    String [] strArray =strLine.split("\\|");

    Do you guys have any advice for me?
    By the way I have looked at opencsv, supercsv and javaCSV libraries but I can't get them to work as the output comes out the same as above. I wish there were some detailed tutorials on how to use these packages.

    I hope my post wasn't too long and boring. Any help is greatly appreciated.
    Last edited by jetnor; 02-05-2011 at 04:47 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Using the code tags makes your code much more readable. By CSV I presume you mean 'comma-separated'? If so, then why split using a vertical bar and not a comma? If you want the values within the array that you've split, then loop over it and print out its elements. If you want to convert a String to a primitive value, look at the API for the wrapper object of the primitive (in particular the parse* function). For example
    Java Code:
    double val = Double.parseDouble(stringValue);
    Last edited by doWhile; 02-05-2011 at 02:56 AM.

  3. #3
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default

    thanks for your quick and useful input. Now that i know what the message printed out to the console means I am using
    Java Code:
    //Print out the arrays of strings
          System.out.println(Arrays.toString(strArray));
    I will now have to figure out a way of doing analysis on the data. If you don't mind can you please have a look at my class and see if you can suggest a way of adding a while loop which excludes the first value of all my arrays as they are string values. I tried using your parse to double idea but I don't think I am doing it correctly. My problem is that the main method can't return anything so I'm not sure how to bypass this.
    Java Code:
    package fileReader;
    import java.io.*;
    import java.util.Arrays;
    
    
    public class FileReader {
    
    	
    	
       public static void main(String args[])
      {
          try{
        // Open the file that is the first 
        // command line parameter
        FileInputStream fstream = new FileInputStream("D:\\faith.csv");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
      
        //Read File Line By Line
        while ((strLine=br.readLine()) != null)   {
       
        	//Split the string using regex
        	
        	String [] strArray =strLine.split("\\,");
        
        	//Print out the arrays of strings
        System.out.println(Arrays.toString(strArray));
      }
      
    
       	
        //Close the input stream
        in.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
       
      }
       
       public double [] convertToDouble() {
    	   for(int i= 1; i<strArray.length; i++){
    			double val= Double.parseDouble(strArray[i]); 
    	   }
    	   return strArray;
       }
       
    }
    Once again thanks.
    Last edited by jetnor; 02-05-2011 at 04:46 AM.

  4. #4
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 10-20-2010, 03:47 PM
  2. Error on reading text from a file.
    By ethann_91 in forum New To Java
    Replies: 1
    Last Post: 01-19-2010, 03:13 PM
  3. error in reading objects from file
    By sakshi in forum New To Java
    Replies: 3
    Last Post: 07-27-2009, 08:17 AM
  4. Error Message when reading an input file.
    By Deluyxe in forum New To Java
    Replies: 8
    Last Post: 04-26-2009, 04:02 PM
  5. reading from a zip file, error
    By Mr tuition in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2008, 12:39 AM

Tags for this Thread

Posting Permissions

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