1 2 Previous Next 23 Replies Latest reply: Sep 14, 2008 9:39 PM by 801489 RSS

    Filling parallel arrays with input from text file.

    801489
      Hello,
      I posted earlier about a grade class in "digits to numbers" and I am now working on an application for it. I need to create a set of parallel arrays to hold 60 entries; an array of student ids, an array of grade instances. For this I need to use the data from a text file to load the arrays. In the text file the fields are (id, name, each score) and are in the CSV format. I know I will have to split() this students information to move along furthur. The part I do not get is How do I use the data from the text file to fill the array?

      I mean...use file reader? or scanner? and if I pick one of them, how do i fill just student ids from the whole text file? and how about the grade instances? I am confused. If anyone will point me in the right direction I would appreciate it.

      Thanks-
        • 1. Re: Filling parallel arrays with input from text file.
          801489
          Here's the code for the Grade class just incase and yes this is Homework.
          public class Grade
          {
               private int studentID;
               private String name;
               private double[] scores;
               private double average;
               private char letterGrade;
           
               /** This constructor gets the student's id, name and array of quiz scores
               */
               public Grade(int studentID, String studentName, double[] quizScores)
                    throws IllegalArgumentException
               {
                    if( studentID < 0 || studentID > 9999)
                    {
                         throw new IllegalArgumentException("Invalid Student Id" + studentID);
                    }
                    this.studentID = studentID;
                    name = studentName;
                    scores = new double[quizScores.length];
                    for (int index = 0; index < quizScores.length; index++)
                    {
                         if(quizScores[index] < 0.0 || quizScores[index] > 110.0)
                         {
                              throw new IllegalArgumentException("Invalid Quiz Score"
                                   + quizScores);
                         }
                         scores[index] = quizScores[index];
                    }
               }
           
               /**This method returns the student ID
                    @return The student ID
               */
               public int getstudentID()
               {
                    return studentID;
               }
           
               /**This method returns the student Name
                    @return The student Name
               */
               public String getName()
               {
                    return name;
               }
           
               /** This method returns the quiz scores
                    @return the quiz scores
               */
               public double[] getQuizScores()
               {
           
                    return scores;
               }
           
               /** This method computes, returns the average and drops the lowest score
                    @return The computed average without the lowest score
               */
           
               public double getAverageQuizScore()
               {
                    double total = 0;
                    double lowest = getLowest();
           
                    for(int index = 0; index < scores.length;index++)
                    {
                         total += scores[index];
                    }
                    total -= lowest;
                    average = total / (scores.length -1);
                    return average;
               }
           
               /** This method computes the average quiz letter grade
                    @return The letter grade for average quiz scores minus the lowest score
               */
           
               public char getAverageQuizGrade()
               {
                    if (average < 60)
                    {
                         letterGrade = 'F';
                    }
                    else if (average < 70)
                    {
                         letterGrade = 'D';
                    }
                    else if (average < 80)
                    {
                         letterGrade = 'C';
                    }
                    else if (average <90)
                    {
                         letterGrade = 'B';
                    }
                    else if (average > 90)
                    {
                         letterGrade = 'A';
                    }
                    return letterGrade;
               }
           
               /** This methods gets the lowest score
                    @return The lowest quiz Score
               */
               private double getLowest()
               {
                    double lowest = scores[0];
           
                    for(int index = 1; index < scores.length; index++)
                    {
                         if(scores[index] < lowest)
                         {
                              lowest = scores[index];
                         }
                    }
                    return lowest;
               }
           
               /** This method returns the string representation of object
                    @return String representation of object
               */
               public String toString()
               {
                    String str = "studentID = " + Integer.toString(getstudentID())
                         + "; Average Quiz Score = " +
                              Double.toString(getAverageQuizScore());
                    return str;
               }
          }
          Edited by: pranavss11 on Sep 14, 2008 5:37 AM
          • 2. Re: Filling parallel arrays with input from text file.
            843785
            If you are less than 60 you go from a D to an F. ouch.
            • 3. Re: Filling parallel arrays with input from text file.
              843785
              pranavss11 wrote:
              Hello,
              Hi
              I mean...use file reader? or scanner?
              I would use BufferedReader with FileReader and use split to break up the fields.
              how do i fill just student ids from the whole text file?
              Read the file and ignore the bits you don't want.
              and how about the grade instances?
              Read the file and ignore the bits you don't want.
              I am confused.
              Ain't we all. :)
              • 4. Re: Filling parallel arrays with input from text file.
                801489
                How do I "ignore the bits I don't want"?

                Thanks for the quick reply.
                Example of the file:
                (StudentsID, Name,quiz scores)
                0007, Treasa, 55, 60, 52, 52

                EDIT: Nevermind I think I understand what you mean! Thank you!!!!!
                Edited by: pranavss11 on Sep 14, 2008 5:47 AM

                Edited by: pranavss11 on Sep 14, 2008 5:48 AM

                Edited by: pranavss11 on Sep 14, 2008 6:05 AM
                • 5. Re: Filling parallel arrays with input from text file.
                  843785
                  I need to create a set of parallel arrays to hold 60 entries; an array of student ids, an array of grade instances.
                  Dont use parallel arrays. Just use 1 array, or even better a list(Why better? Cause a list dont have a limited size, like the array, that will be limited to 60).
                  For this I need to use the data from a text file to load the arrays.
                  Okay, for this latest .txt data format;
                  (StudentsID, Name,quiz scores)
                  0007, Treasa, 55, 60, 52, 52
                  Create a new class called Student, which has the feilds: int id, String name, ArrayList quizScores(ArrayList for several quizScore results). And create the propper getter and setter/add methods for these.
                  This student object you can then store in your array, or list for the 60 entries you mentioned first, instead of having parallel arrays.
                  I know I will have to split() this students information to move along furthur. The part I do not get is How do I use the data from the text file to fill the array?
                  If this is the data(1 of the lines in the .txt): 0007, Treasa, 55, 60, 52, 52. Then after reading a line from the .txt file, use split(", "); Then put the information in the Student(class) object, and store it in your array/list.

                  Also:
                  How do I "ignore the bits I don't want"?
                  Just dont make use of that information. When you use split(), you will get an String[] array. Just use the parts of that array you want to use and "ignore" the rest.
                  • 6. Re: Filling parallel arrays with input from text file.
                    801489
                    I can't do that tho.. Its not in the specifications. I have to create a set of parallel arrays( instructor) and I can't create an extra class =/
                    • 7. Re: Filling parallel arrays with input from text file.
                      843785
                      pranavss11 wrote:
                      I can't do that tho.. Its not in the specifications. I have to create a set of parallel arrays( instructor) and I can't create an extra class =/
                      ...Your instructor is gay! Why is he teaching stupid programming(Woops 1 of the X parallel arrays got fucked up, all(most of them depending on where the array got F. upp) program entries are all of the suddely faulty.)? And why is he prohibiting you from doing things in a better manner? Nvm, not questions for you to answere I suppose :/.
                      • 8. Re: Filling parallel arrays with input from text file.
                        801489
                        heh. Using partially filled arrays is 10% of the grade for this assignment.
                        • 9. Re: Filling parallel arrays with input from text file.
                          843785
                          prigas wrote:
                          ...Your instructor is gay!
                          That's an insult to gay people. I would have said something else which would have been an insult to stupid people. :)
                          • 10. Re: Filling parallel arrays with input from text file.
                            801489
                            Ok I am lost. The application was use grade class to do the following.
                            1. Create a set of parallel arrays to hold entries
                            a. An array of student IDS
                            b. An array of Grade instances.
                            2. Use the data in the file" CS076A01_input.txt" to load the arrays.
                            a. The maximum of students is 60. We will be testing with a much smaller number of student, but your program should be able to handle up to 60. It should also protet itself in case there are more than 60 records in the input file.
                            b. The number of quizzes can vary from student to student.
                            c. All of a student's information (id, name , quiz scores) are on one "line".
                            d. The fields ( id, name , each score) are seperated with a ","
                            e. Discard any records where the student ID is a duplicate of previous record and write a record to system.err with a message that is the record is a duplicate.
                            f. Discard any record for which the Grade constructor throws IllegalArgumentException and write a record to System.err with a message that the record is invalid; include the contect of the record. This means there is NO entry for the record in the studentID array or the Grade array.
                            3. Display a JOptionPane input dialog into which the user will enter a student ID.
                            a. Search for a matching Id the array of Student IDS
                            i. If Student Id is not in the array use JoptionPane display dialog to tell the user that the id entered is not valid and request another ID.
                            ii. If student id is in the array uise the Grade Instance to display ,'
                            b. After processing the user's entry, repeat this until the user clicks on the dialog's "cancel" button.

                            Now the problem I am having is what I thought I understood. I read the file, I loop it and split it. The question is HOW DO I GET JUST THE STUDENT IDS FOR THE studentID array??? Here's the code I have finished till now. I still don't get the "ignore" part.
                            import java.util.Scanner;
                            import java.io.*;
                            
                            public class GradeApp
                            {
                                 public static void main (String[] args) throws IOException
                                 {
                                      int[] studentID = new int[60];
                                      Grade[] grade = new Grade[60];
                                      String str;
                                      int index = 0;
                            
                                      FileReader freader = new FileReader ("CS076A01_INPUT.txt");
                                      BufferedReader inputFile = new BufferedReader (freader);
                                      str = inputFile.readLine();
                                      while (str != null)
                                      {
                                           str = inputFile.readLine();
                                           String[] tokens = str.split(",");
                                      }
                                 }
                            
                            }
                            Heres the contect of the Input file
                            0007, Treasa, 55, 60, 52, 52
                            7777, Zain, 79, 75, 77, 77, 75
                            007, Carlos, 83, 88, 85, 82
                            7777, Narayan, 99, 92, 94, 97
                            1007, Hiep, 71, 60, 65, 73, 68
                            12345,Phung, 75, 85, 95, 65
                            321O, Madison, 105, 95, 85
                            1234, Ruth, 25, 90, 91


                            Any suggestions will be appreciated.

                            Edited by: pranavss11 on Sep 14, 2008 3:13 PM
                            • 11. Re: Filling parallel arrays with input from text file.
                              843785
                              Where do you actually put the data into the arrays? and where's your student name String array?

                              edit: never mind that second question. I see that the Grade object should take care of this. Nevertheless, you never place a Grade object into your Grade array.

                              Edited by: Encephalopathic on Sep 14, 2008 3:39 PM
                              • 12. Re: Filling parallel arrays with input from text file.
                                801489
                                Can you explain a bit more?
                                • 13. Re: Filling parallel arrays with input from text file.
                                  843785
                                  pranavss11 wrote:
                                  Can you explain a bit more?
                                  You have two arrays in this code an int array and a Grade array, that just sit there, arrays that are supposed to hold your data, and yet they never have data placed into them. I thought that this was the point of your exercise. Just what about this is unclear?
                                  • 14. Re: Filling parallel arrays with input from text file.
                                    801489
                                    I do not get the part about how to ignore the rest and just put in the studentIds in the studentID array. Do I use the token thing I split and then Do I specify the address like token [0] has the first Student ID then token[6] and so on.
                                    1 2 Previous Next