Hi DaniWeb! I kinda need your help on this little bugger. How can I input this CSV file in to an array?

Module Name, Module Code -- Student ID, Last Name, First Name, Exam Score, Average

Introduction to Art Life, ITAL
07,Krave,Mark,27,30
60,Lever,Paul,89,96
13,Green,Jon,15,40
15,Davids,Jerry,5,15

If the Module Name and Module Code didn't exist I would have done this myself. >_>

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

Sorry about that. This is what I did. I load the CSV file using the data showing on the first post.
My goal is make Course Name and Course ID separate in each JTextField, and the rest of the data displayed in a JTable.

            JFileChooser jfc = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "CSV files (*csv)", "csv");
                jfc.setFileFilter(filter);
                int ret = jfc.showOpenDialog(testy3.this);
                if(ret == JFileChooser.APPROVE_OPTION)
                {
                    File f = jfc.getSelectedFile();
                    String s = f.getAbsolutePath();
                   try
                    {

                            //csv file containing data
                            String strFile = s;

                            //create BufferedReader to read csv file
                            BufferedReader br = new BufferedReader( new FileReader(strFile));
                            String strLine = "";
                            StringTokenizer st = null;
                            int cName = 0, cID = 0, sID = 0, sLast = 0, sFirst = 0, sExam = 0, sAver = 0;
                            //Course Name, Course ID, Student ID, Last Name, First Name, Exam , Average


                            //read comma separated file line by line
                            while( (strLine = br.readLine()) != null)
                            {

                                    //break comma separated line using ","
                                    st = new StringTokenizer(strLine, ",");

                                    while(st.hasMoreTokens())
                                    {
                                            //display csv values
                                           if(cName == 0){
                                                cName1.setText (st.nextToken().toString());
                                                cName++;
                                            } else if (cID == 0){
                                                cID1.setText (st.nextToken().toString());
                                                cID++;
                                            } else if (sID == 0){
                                                sID1.setText (st.nextToken().toString());
                                                sID++;
                                            } else if (sLast == 0){
                                                sLast1.setText (st.nextToken().toString());
                                                sLast++;
                                            } else if (sFirst == 0){
                                                sFirst1.setText (st.nextToken().toString());
                                                sFirst++;
                                            } else if (sExam == 0){
                                                sAver1.setText (st.nextToken().toString());
                                                sExam++;
                                            } else if (sAver == 0){
                                                sAver1.setText (st.nextToken().toString());
                                                sAver++;
                                            }
                                    }

                                    //reset everything!
                                    cName = 0;
                                    cID = 0;
                                    sID = 0;
                                    sLast = 0;
                                    sFirst = 0;
                                    sAver = 0;
                                    sExam = 0;

                            }


                    }
                    catch(Exception e1)
                    {
    System.out.print("Error");

                    }
                }
            }

Not sure what your problem is here...
to read the course info just add another read/tokenise before you enter the loop (line 20)
to store in an array just declare an array with "enough" rows, and columns for each of the variables, and declare an int to count the rows you have used (initially zero)
where exactly are you getting stuck?

  • 10 open file
  • 20 start loop
  • 30 read line
  • 40 if end of file goto 100
  • 50 parse line
  • 60 goto 30
  • 100 end loop
Member Avatar for 1stDAN

Hi Edward,

I have digged up an exercise of my old former Java course and slightly modified to fit your notion, I have also made use of your example data omitting your first input line.

Back then we had to program a csv-reader using an 2D ArrayList to store all elements of the two-dimensional csv-separated input data.

However that be, I cannot post the complete solution, for this won't be well received on daniweb, so I'll only show important statements. Omitted statements has been marked ....

public class CSVReaderTokenizer {
  public static void main(String[] args) throws IOException {
    ...
    ...
    ...
    ArrayList<ArrayList<String>> ar = new    
     ArrayList<ArrayList<String>>(); // Token are stored in 2D ArrayList

    try {
      file = new BufferedReader(new FileReader(csvFile)); // csv file

      // Loop over all rows of the csv file
      ...
      ...
      while (( line = file.readLine() ) != null) {
        ...
        ...     
        // Add a new row to the two-dimensional ArrayList
        ar.add(new ArrayList<String>(5));    

        // Add all tokens of an input line to the ArrayList
        int col = 0;
        while (st.hasMoreTokens()) { // the very gist of the matter :)
          String net = st.nextToken().trim();
          ar.get(row).add(col, net); // not easy to understand
          col++; 
        }
      row++;
      }

     // Show content of two-dimensioal ArrayList
     System.out.println("\nContent of arrayList");
     for ( int r = 0; r < row; r++) {
      for ( int c = 0; c < ar.get(r).size(); c++)
        ...
        ...
        ...
      }
    }
    catch (IOException e){
      ...
      ...
      ...
    }
    finally {
      ...
    }
  }  
}
/* Result:
C:>java CSVReaderTokenizer
Content of arrayList
0       0       07
0       1       Krave
0       2       Mark
0       3       27
0       4       30

1       0       60
1       1       Lever
1       2       Paul
1       3       89
1       4       96

2       0       13
2       1       Green
2       2       Jon
2       3       15
2       4       40

3       0       15
3       1       Davids
3       2       Jerry
3       3       5
3       4       15
*/

Because one usually don't know the dimensions of a csv file, I think that storing elements of a csv file in a dynamic Arraylist is much better than store them in a plain static array.

Member Avatar for 1stDAN

Sorry, question, why I am unable to change above post? Time elapsed almost 25 minutes.

Thanks for helping

Member Avatar for 1stDAN

Hi to everybody in there!

At the risk of getting again down voted, I am just full of curiosity to get to know why above postes have been down voted.

A fair reply isn't that necessary, also I don't care downvotes anymore, for I am definitely leaving once sincere daniweb.

Good bye!

Member Avatar for 1stDAN

Thank you so much!

Ah, didn't you see there is just another chance to spank your monkey:
Click Here

Do it again, vile baby!

Hi 1stDAN

Votes without comments are, by design, anonymous. If someone wants to hide behind that then you really shoudn't let them wind you up. Just ignore it and move on; don't feed the trolls!
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.