Results 1 to 15 of 15
  1. #1
    arnonneder is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default reading a file, sorting it and displaying its content on screen

    Hi,

    I have a program that writes a user's input to a file.
    The input consists of several loops. In each loop the user enters a student number, the student first name and last name and then several exam results. Each student has different number of exams, depend on the user input.

    For instance, the input can be (explanations are in brackets but not in the actual file):
    505 (student number)
    efron (last name)
    ben (first name)
    3 (no. of exams)
    88 (1st exam mark)
    70 (2nd exam mark)
    100 (3rd exam mark)
    38(student number)
    agron (last name)
    sally (first name)
    5 (no. of exams)
    78 (1st exam)
    60 (2nd exam)
    90 (3rd exam)
    88 (4th exam)
    89 (5th exam)
    101( student number)
    ... and so on

    I need to write a program that reads that file and writes it to the screen, but it needs to do it in ascending order according to the student number.
    Leaving aside the format it will be displayed on the screen, my question is how should I sort the data by the student number?
    I though about first reading each student as an array, but my problem is that each array contains different number of elements (depends on the number of exams) and also each array containד both strings (first and last names) and ints.
    Does anyone have an idea?

    Thanks

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: reading a file, sorting it and displaying its content on screen

    There are several possibilities. Assuming you have to use arrays, I would use a two dimensional array for the test scores. You keep track by treating the two dimensional array as a one dimensional array while sorting. For example:

    Java Code:
    int [][] a = new int[2][];
    int []temp;
    a[0] = new int[]{99,100,77,88};  // test scores for first student.
    a[1] = new int[]{97,82, 99}; // test scores for second student.
    
    // you can swap the scores like this:
    
    temp = a[0];
    a[0] = a[1];
    a[1] = temp;
    You could also just concatenate the scores together into a string. But I prefer the first way.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: reading a file, sorting it and displaying its content on screen

    I though about first reading each student as an array, but my problem is that each array contains different number of elements (depends on the number of exams) and also each array containד both strings (first and last names) and ints.
    It sounds like each student is a object holding multiple types of data. What I've always done in this situtaion is create another class. Then you can create an array of objects made from that class.
    With the right know how, anything is possible

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: reading a file, sorting it and displaying its content on screen

    If that's allowed, I would agree. But the OP's post didn't mention classes only arrays. And there would still need to be an array or List implementation within the class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: reading a file, sorting it and displaying its content on screen

    First of I think Jim was right with the multiDimensional arrays but if that doesn't work you could just use a bunch like this.
    Java Code:
    int[] numberOfTest;
             int[][] testScores = int[numberOfStudents][numberOfTest];
             String[] firstName;
             String[]lastName;[
    So you could think of students as numbers. Number 0 in all the arrays applies to one person. Now you said you had the problem of students taking different number of exams well then you just put in a int to tell how many they took. I have the multiDimensinal Array because it would be like testScores[forWhatStudend][whichTest]. Hopefully this is clear enought and can fix your problem
    With the right know how, anything is possible

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: reading a file, sorting it and displaying its content on screen

    The number of tests vary. besides, its not necessary to have a square or rectangular two-D matrix. A two-D matrix is nothing more than a a one-D matrix which holds other one-D matrices of arbitrary length.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    arnonneder is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: reading a file, sorting it and displaying its content on screen

    Thank you all for your comments.

    What I didn't mention, is that each student has a different number of subjects, and each subject has different number of exams. I thought about two dimensional array, but that sounds like it's not enough..

    Any ideas?
    Thanks!

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: reading a file, sorting it and displaying its content on screen

    Ugh. More requirements. Now I tend to think that a classes are the way to go. You could have a Student class with appropriate information (exercise for you). Then you could possibly have a Subject class which also holds appropriate information.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: reading a file, sorting it and displaying its content on screen

    could you make an array of an array of an array. Like a 3d array. The first being student. The second there subject. The third their scores
    With the right know how, anything is possible

  10. #10
    arnonneder is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: reading a file, sorting it and displaying its content on screen

    Thanks again for your help!

    I'm trying to use 3d array, but the problem is that I can only define the number of subjects for each student and number of exam for each subject while reading the file, and not initially. When I want to print on the screen, for instance, the value stored in homeworkMark[0][1][1] I can't, because the number of elements is defined inside the loop and not outside of it.

    This is what I have so far:

    Scanner diskScanner = new Scanner(new File("cookedData.txt"));
    int NoOfStudents = diskScanner.nextInt();

    int StudentNumber[] = new int[NoOfStudents];
    String FamilyName[] = new String[NoOfStudents];
    String FirstName[] = new String[NoOfStudents];
    int NoOfSubjects[] = new int[NoOfStudents];
    int subject[][];
    int homeworkMark[][][];
    int examMark[][][];
    for (int j = 0; j < NoOfStudents; j++)
    {

    StudentNumber[j] = diskScanner.nextInt();
    diskScanner.nextLine();
    FamilyName[j] = diskScanner.nextLine();
    FirstName[j] = diskScanner.nextLine();
    NoOfSubjects[j] = diskScanner.nextInt();
    subject = new int[NoOfStudents][NoOfSubjects[j]];

    for(int i = 0; i < NoOfSubjects[j]; i++)
    {

    subject[j][i] = diskScanner.nextInt();
    int NoOfAssignments = diskScanner.nextInt();
    homeworkMark = new int[NoOfStudents][NoOfSubjects[j]][NoOfAssignments];
    examMark = new int[NoOfStudents][NoOfSubjects[j]][NoOfAssignments];

    for(int y = 0; y < NoOfAssignments; y++)
    {
    homeworkMark[j][i][y] = diskScanner.nextInt();
    examMark[j][i][y] = diskScanner.nextInt();
    }
    }
    }
    diskScanner.close();
    System.out.println(StudentNumber[0]);
    System.out.println(FamilyName[0]);
    System.out.println(FirstName[0]);
    System.out.println(NoOfSubjects[0]);
    System.out.println(subject[0][1]);

    There is a compile error because of the last value I want to print- "subject[0][1]" because the number of elements were defined inside the loop and not outside of it...

    Thanks again!

  11. #11
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: reading a file, sorting it and displaying its content on screen

    Is there a max limit of exams possibly taken? Becuase if so initiate the arrays with the max possible and set all of the to -1. Then when you go to print use a if statement so it only prints whats not -1. This may not be the most memory efficient but it should work Or if that doesnt work then have a loop find were the -1 and use that as a limit by having a varialbe like int limit[]
    Last edited by Daryn; 01-19-2014 at 08:14 PM.
    With the right know how, anything is possible

  12. #12
    arnonneder is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: reading a file, sorting it and displaying its content on screen

    Yes, there is a limit to the number of subjects and number of exams. I used your suggestion and it seems to be working. Thanks!

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: reading a file, sorting it and displaying its content on screen

    As I said earlier, you don't have to do that. A 2d array is nothing but an array of 1d arrays. So:
    Java Code:
          int[][] a = {{1,2,3},{10,20,30,40,50},{100,200}};
          for (int j = 0; j < a.length; j++) {
               for (int k = 0; k < a[j].length; k++) {
                  System.out.print(a[j][k] + " ");
               }
               System.out.println();
          } 
          // or this
          for(int j = 0; j < a.length; j++) {
             System.out.println(Arrays.toString(a[j]));
          }
    And if you want to grow an array, then read about Arrays.copyOf().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: reading a file, sorting it and displaying its content on screen

    Quote Originally Posted by jim829 View Post
    As I said earlier, you don't have to do that. A 2d array is nothing but an array of 1d arrays. So:
    Java Code:
          int[][] a = {{1,2,3},{10,20,30,40,50},{100,200}};
          for (int j = 0; j < a.length; j++) {
               for (int k = 0; k < a[j].length; k++) {
                  System.out.print(a[j][k] + " ");
               }
               System.out.println();
          } 
          // or this
          for(int j = 0; j < a.length; j++) {
             System.out.println(Arrays.toString(a[j]));
          }
    And if you want to grow an array, then read about Arrays.copyOf().

    Regards,
    Jim
    But you would need an array for all the students inside of that you would need an array with all there subjects then inside that all the grades.
    With the right know how, anything is possible

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Re: reading a file, sorting it and displaying its content on screen

    It doesn't matter. If you really want to use 3d arrays or 4d arrays you can use the length of each array object to determine the size of that object. And you can reallocate the array using the Arrays.copyOf() method. So you do not need any flag to signify the last used element. Actually, I would recommend using ArrayList because it does all of that for you.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Reading content from an ftp file
    By igorland in forum New To Java
    Replies: 8
    Last Post: 01-28-2013, 03:38 AM
  2. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  3. Replies: 21
    Last Post: 01-27-2012, 05:37 AM
  4. Reading text file and ouput to screen problem
    By joshhazel in forum New To Java
    Replies: 5
    Last Post: 12-31-2011, 09:54 PM
  5. Reading structured content from PDF file
    By chanduk in forum Advanced Java
    Replies: 1
    Last Post: 12-09-2010, 01:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •