read csv file and make array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bela
    New Member
    • Sep 2007
    • 13

    read csv file and make array

    Hello,

    I am very new to JAVA.

    I would like to know, how to read *.csv file from java and how to save that data into an array.

    regards,
    bela
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    Originally posted by bela
    Hello,

    I am very new to JAVA.

    I would like to know, how to read *.csv file from java and how to save that data into an array.

    regards,
    bela
    Hi,
    First download the javacsv.jar. After that read a bit about csvReader API.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3

      Your answer is really Impressive.
      As soon as I saw this I went through Test it.
      But failed.
      Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
      But this line is not working as mentioned in your Link.
      [code=java]
      import au.com.bytecode .opencsv.*;
      [/code]
      Please help.

      Kind regards,
      Dmjpro.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by dmjpro
        Your answer is really Impressive.
        As soon as I saw this I went through Test it.
        But failed.
        Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
        But this line is not working as mentioned in your Link.
        [code=java]
        import au.com.bytecode .opencsv.*;
        [/code]
        Please help.

        Kind regards,
        Dmjpro.
        Sorry I got it.
        Actually what it mentioned in your link that the CSVReader class is under
        au.com.bytecode .opencsv package.
        But what I saw in NetBeans is, com.csvreader.
        Anyway, I think it is Jar specific.

        Kind regards,
        Dmjpro.

        Comment

        • madhoriya22
          Contributor
          • Jul 2007
          • 251

          #5
          Originally posted by dmjpro
          Your answer is really Impressive.
          As soon as I saw this I went through Test it.
          But failed.
          Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
          But this line is not working as mentioned in your Link.
          [code=java]
          import au.com.bytecode .opencsv.*;
          [/code]
          Please help.

          Kind regards,
          Dmjpro.
          Hi,
          I think u have not put the jar at right place. Put that jar in Libraries folder of ur project. That line should work. U can try with this line also
          Code:
           
          import com.csvreader.CsvReader;
          and use the methods given this class to read the CSV file.

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by madhoriya22
            Hi,
            I think u have not put the jar at right place. Put that jar in Libraries folder of ur project. That line should work. U can try with this line also
            Code:
             
            import com.csvreader.CsvReader;
            and use the methods given this class to read the CSV file.
            I think you didn't see my Last Post.
            I posted my solution there.
            Actually, I used the package what is mentioned in your link.
            Check that link.

            Kind regards,
            Dmjpro.

            Comment

            • madhoriya22
              Contributor
              • Jul 2007
              • 251

              #7
              Originally posted by dmjpro
              I think you didn't see my Last Post.
              I posted my solution there.
              Actually, I used the package what is mentioned in your link.
              Check that link.

              Kind regards,
              Dmjpro.
              Hi,
              Check with this link again ....

              This is the different and right one.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dmjpro
                I think you didn't see my Last Post.
                I posted my solution there.
                Actually, I used the package what is mentioned in your link.
                Check that link.

                Kind regards,
                Dmjpro.
                Just for the fun of it I downloaded and installed the .jar and api docs. After reading
                the API docs and the example code (AddressExample .java: the demo reads
                the addresses.csv file) I decided to add the .jar to my classpath and it just
                worked (as it should). Please read before you simply click away.

                kind regards,

                Jos

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  Just for the fun of it I downloaded and installed the .jar and api docs. After reading
                  the API docs and the example code (AddressExample .java: the demo reads
                  the addresses.csv file) I decided to add the .jar to my classpath and it just
                  worked (as it should). Please read before you simply click away.

                  kind regards,

                  Jos
                  Done it too.
                  The reading part didn't kill me either.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    Done it too.
                    The reading part didn't kill me either.
                    Of course not; why should it? The magic word is reading again.

                    kind regards,

                    Jos

                    Comment

                    • bela
                      New Member
                      • Sep 2007
                      • 13

                      #11

                      thank you.
                      But I'm not able to convert in / store as in array form.

                      regards,
                      bela

                      Comment

                      • madhoriya22
                        Contributor
                        • Jul 2007
                        • 251

                        #12
                        Originally posted by bela
                        thank you.
                        But I'm not able to convert in / store as in array form.

                        regards,
                        bela
                        Hi,
                        Show us a bit what u have tried ? Then only we vil be able to help you.

                        Comment

                        • snowfall
                          New Member
                          • Aug 2007
                          • 56

                          #13
                          Hi, I tried an Example but am not able to read the file.
                          My Code is,

                          mport com.csvreader.C svReader;
                          import java.io.*;
                          class csveg
                          {
                          public static void main(String a[])
                          {
                          try
                          {
                          CsvReader cs=new CsvReader("hi.c sv");
                          System.out.prin tln("hiii "+ cs);
                          System.out.prin tln("hiii "+ cs.getColumnCou nt());//Prints 0
                          String arra[]=cs.getValues() ;
                          System.out.prin tln("hiii "+arra.leng th);//Prints 0
                          for(int j=0;j<arra.leng th;j++)
                          {
                          System.out.prin tln("hiii "+j);
                          System.out.prin tln(arra[j]);
                          }


                          }catch(Exceptio n e){System.out.p rintln("Excepti on is ");}
                          }
                          }

                          pls help...

                          Comment

                          • madhoriya22
                            Contributor
                            • Jul 2007
                            • 251

                            #14
                            Originally posted by snowfall
                            Hi, I tried an Example but am not able to read the file.
                            My Code is,

                            mport com.csvreader.C svReader;
                            import java.io.*;
                            class csveg
                            {
                            public static void main(String a[])
                            {
                            try
                            {
                            CsvReader cs=new CsvReader("hi.c sv");
                            System.out.prin tln("hiii "+ cs);
                            System.out.prin tln("hiii "+ cs.getColumnCou nt());//Prints 0
                            String arra[]=cs.getValues() ;
                            System.out.prin tln("hiii "+arra.leng th);//Prints 0
                            for(int j=0;j<arra.leng th;j++)
                            {
                            System.out.prin tln("hiii "+j);
                            System.out.prin tln(arra[j]);
                            }


                            }catch(Exceptio n e){System.out.p rintln("Excepti on is ");}
                            }
                            }

                            pls help...
                            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.

                            Comment

                            • bela
                              New Member
                              • Sep 2007
                              • 13

                              #15
                              Originally posted by madhoriya22
                              Hi,
                              Show us a bit what u have tried ? Then only we vil be able to help you.
                              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

                              Comment

                              Working...