read csv file and make array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #16
    Originally posted by bela
    yes, my code is :

    import java.io.*;
    import au.com.bytecode .opencsv.CSVRea der;

    public class csv2array {

    private static final String data = "e:\\weather.cs v";

    public static void main(String[] args) throws IOException {

    CSVReader reader = new CSVReader(new FileReader(data ),',','\'',1);
    String [] nextLine;
    int cnt=0;
    String[][] str1= new String[14][5];

    while ((nextLine = reader.readNext ()) != null) {
    System.out.prin tln("Row no is : " + cnt);
    System.out.prin tln(nextLine[0]+',' + nextLine[1]+','+ nextLine[2]+',' +nextLine[3]+','+nextLine[4]);
    for(int n=0;n<14;n++){
    for(int rowelement=0;ro welement<5;rowe lement++){
    str1[n][rowelement]=nextLine[rowelement];
    }
    }
    cnt++;
    }

    }
    }

    weather.csv is available on WEKA.
    I'm taking data in matrix str1.
    How to see the containts of str1?
    when I tried, System.out.prin tln(str1); it gives address of str1.

    regards,
    bela
    Hi,
    If u hav done all this hard work urself then printing array values should not be a problem :) ur str1 is a two dimensional array. For reading values in that u hav to put two for loops like..
    Code:
    for(int i = 0; i < 14; i++) {
    	for(int j = 0; j < 5; j++) {//obviously u can get row and column lenghts by using str1.length.
    	 System.out.println(str1[i][j]);
    	}
    }

    Comment

    • bela
      New Member
      • Sep 2007
      • 13

      #17
      read csv file and make array

      Originally posted by madhoriya22
      Hi,
      If u hav done all this hard work urself then printing array values should not be a problem :) ur str1 is a two dimensional array. For reading values in that u hav to put two for loops like..
      Code:
      for(int i = 0; i < 14; i++) {
      	for(int j = 0; j < 5; j++) {//obviously u can get row and column lenghts by using str1.length.
      	 System.out.println(str1[i][j]);
      	}
      }
      yes, this I've tried, and also know this method. But here it prints element by element. my final aim is to make new matrix by extracting few rows and column of this str1 matrix, where I need entire row/ entire column.
      using for loops, I can do it,
      but I was searching for different method if there is as this will be lengthy procedure.
      I have to create those many arrays/matrices.
      Do you know any other method?
      Last edited by bela; Sep 6 '07, 05:43 AM. Reason: to add comment

      Comment

      • snowfall
        New Member
        • Aug 2007
        • 56

        #18
        Originally posted by madhoriya22
        Hi,
        If u hav not read the API for method getColumnCount( ) .. then check it again and try understand what it is saying ....
        It is saying that "Gets the count of columns found in this record".

        Here U hav get the file in the csvReader but havn't started reading it ... So start reading the file then only this method vil give u the column count for the particular record u r reading.

        I hope u r getting my point.
        Ya got it.. Now its executing.. Thnks

        Comment

        • madhoriya22
          Contributor
          • Jul 2007
          • 251

          #19
          Originally posted by snowfall
          Ya got it.. Now its executing.. Thnks
          Hi,
          Ofcourse Welcome Buddy :)

          Comment

          • madhoriya22
            Contributor
            • Jul 2007
            • 251

            #20
            Originally posted by bela
            yes, this I've tried, and also know this method. But here it prints element by element. my final aim is to make new matrix by extracting few rows and column of this str1 matrix, where I need entire row/ entire column.
            using for loops, I can do it,
            but I was searching for different method if there is as this will be lengthy procedure.
            I have to create those many arrays/matrices.
            Do you know any other method?
            Hi,
            Check my post #7. Go to that link. That CsvReader API has more methods than the one which u are using. By Using methods of this u can skip the records which u don't want to read.

            Tip: Dont be rude while asking, sharing or answering. Always be thankful :)

            Comment

            • bela
              New Member
              • Sep 2007
              • 13

              #21
              Originally posted by madhoriya22
              Hi,
              Check my post #7. Go to that link. That CsvReader API has more methods than the one which u are using. By Using methods of this u can skip the records which u don't want to read.

              Tip: Dont be rude while asking, sharing or answering. Always be thankful :)
              hello,

              thank you.
              I'll go through it.

              regards,
              bela

              Comment

              • snowfall
                New Member
                • Aug 2007
                • 56

                #22
                Originally posted by madhoriya22
                Hi,
                Ofcourse Welcome Buddy :)
                one more query :-)

                I used parse mtd of CsvReader and created a new CsvReader obj.
                If i want to write this to a file, what should i do??

                pls help...

                Comment

                • madhoriya22
                  Contributor
                  • Jul 2007
                  • 251

                  #23
                  Originally posted by snowfall
                  one more query :-)

                  I used parse mtd of CsvReader and created a new CsvReader obj.
                  If i want to write this to a file, what should i do??

                  pls help...
                  Hi,
                  Again U have to read a bit for that :) Use CsvWriter class for that. Here is the link for that class API...

                  start using methods of it and have fun :)

                  Comment

                  Working...