1 2 Previous Next 24 Replies Latest reply: Aug 15, 2007 10:54 PM by 807605 RSS

    converting csv file into arrray

    807605
      hey i want to convert a csv file which looks like this
      order,submit ,57868,2367673,,Bar,ramesh,
      ....................................................
      .................................

      i am able to read that but now i want to store the data into variable for example the Order will be stored in vairable order,, and submit whould be get stored in status variable etc,.....

      its confusing to use Stringtokenizer or split which evver method
      i need some specific direction to proceed may be a start or any the class or method which i have to know before i write code i read many tutorials but sometime iunderstand or dont understand so i need a good guidence , please help i dont want the code i just need tips which i can follow and complete

      thanks in advance
      (i know this topic has been prveiously discussed widely i am posting here because i didnt get what i want or its just confusing)
        • 1. Re: converting csv file into arrray
          807605
          Read the file in using a buffered reader

          Split the String with split

          Store the various values in your unidentified class.

          What's the difficulty here? Where are you running into so much confusion?
          • 2. Re: converting csv file into arrray
            807605
            Happy reading

            http://java.sun.com/docs/books/tutorial/essential/regex/
            • 3. Re: converting csv file into arrray
              807605
              i am stuck in splitting the file
              import java.io.*;
              import java.util.*;

              class csv2
              {
                   public static void main(String[] args) throws Exception
                   {
                        String A = null;
                        String []B;
                        try
                        {
                             BufferedReader in = new BufferedReader (new FileReader("input.csv"));
                             while ((A=in.readLine()) != null)
                             {
                                  for (i=0;i<A.length;i++)
                                  {
                                       String B[i] = A.split("[,]");
                                       System.out.println(B[i]+"\t");
                                  }


                             }
                        }
                        finally
                        {

                             System.out.println("Working Good");
                        }
                   }
              }


              its the code looks like
              • 4. Re: converting csv file into arrray
                807605
                Please repost your code using the code tags.

                1) Select the code in the message box.

                2) Click the code button
                • 5. Re: converting csv file into arrray
                  807605
                  thanks cotton i will read the link and try to find the solution :) if i found great great thanks or else i will come back to you.
                  • 6. Re: converting csv file into arrray
                    807605
                    This looks like the problem but it's hard to tell with the formatting being a disaster what exactly you are doing.
                    String B = A.split("[,]");
                    I am not sure what those | are there for. You should get rid of them.
                    • 7. Re: converting csv file into arrray
                      807605
                      String[] values = A.split(",");
                      • 8. Re: converting csv file into arrray
                        807605
                        I'm no expert here, but what happens if you change this:
                            String B = A.split("[,]");
                        to this:
                            String[] B = A.split(",");
                        • 9. Re: converting csv file into arrray
                          807605
                          Please repost your code using the code tags.

                          1) Select the code in the message box.

                          2) Click the code button
                          import java.io.*;
                          import java.util.*;

                          class csv2
                          {
                               public static void main(String[] args) throws Exception
                               {
                                    String A = null;
                                    String []B;
                                    try
                                    {
                                         BufferedReader in = new BufferedReader (new FileReader("input.csv"));
                                         while ((A=in.readLine()) != null)
                                         {
                                              for (i=0;i<A.length;i++)
                                              {
                                                   String B[i] = A.split("[,]");
                                                   System.out.println(B[i]+"\t");
                                              }


                                         }
                                    }
                                    finally
                                    {

                                         System.out.println("Working Good");
                                    }
                               }
                          }

                          i have just started please give me guidance
                          • 10. Re: converting csv file into arrray
                            807605
                            Please repost your code using the code tags.

                            1) Select the code in the message box.

                            2) Click the code button
                            i have just started please give me guidance
                            um.... the recommendations above are pretty clear....

                            but to spell it out:

                            1. type your message and paste any code into the Message box.
                            2. Highlight a block of code in the message box.
                            3. press the "code" button just above the message box.
                            4. Repeat steps 2 and 3 for each block of code.
                            5. Click the "post" button to submit your message.

                            Message was edited by:
                            petes1234
                            • 11. Re: converting csv file into arrray
                              807605
                              actually my csv files looks like this
                              order,submit,123,456.0,890,rajesh,---> transaction
                              .............................................
                              .................................

                              i am trying to read every line as one transaction.
                              and then trying to store each value in seperate attributes ie every column in one attribute.then i have to sort out the records based on price and volume.It is my assignment and now as a first step i am trying to read the csv file and store it in variables.
                              import java.io.*;
                              import java.util.*;
                              
                              class csv2
                              {
                                   public static void main(String[] args) throws Exception
                                   {
                                        String A = null;
                                        String []B;
                                        try
                                        {
                                             BufferedReader in = new BufferedReader (new FileReader("input.csv"));
                                             while ((A=in.readLine()) != null)
                                             {
                                                  for (i=0;i<A.length;i++)
                                                  {
                                                  String B[i] = A.split("[,]");
                                                       System.out.println(B[i]+"\t");
                                                  }
                              
                              
                                             }
                                        }
                                        finally
                                        {
                              
                                             System.out.println("Working Good");
                                        }
                                   }
                              }
                              • 12. Re: converting csv file into arrray
                                807605
                                This code doesn't make any sense. For one thing see replies 7 and 8 for the fix for your split.

                                But then... throw B in the rubbish bin. Use a List and for each line store the array returned by split in the List.
                                • 13. Re: converting csv file into arrray
                                  807605
                                  I'm ot sure if anyone has said this yet but don't put the spit inside a loop. You split once and then use a loop to iterate over the resulting array.
                                  spilt first
                                  loop afterwards
                                  • 14. Re: converting csv file into arrray
                                    807605
                                    This code doesn't make any sense. For one thing see
                                    replies 7 and 8 for the fix for your split.

                                    But then... throw B in the rubbish bin. Use a List
                                    and for each line store the array returned by split
                                    in the List.
                                    oh yes i understand it doesnt make sense , i tried the options in replies 7 and 8 but it doesnt work. i read the tutorials link which u send i , iam not getting any direction in that how to proceed .....
                                    please help me guys
                                    1 2 Previous Next