Please help me fix this code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elbone2k
    New Member
    • Dec 2009
    • 3

    Please help me fix this code

    I'm trying to write a program that read a file containing student data in this format
    Code:
    studntId | Name     | Major                | GPA  | YEAR    |
    ---------|----------|----------------------|------|---------|
    123456   | John Doe | Computer Science     | 3.5  | Junior  |
    789012   | Mary Boe | Computer Information | 3.7  | Senior  |

    the program is supposed to read the file and organize the data according to the students last name and look like this
    Code:
    studntId | Name     | Major                | GPA | YEAR    |
    ---------|----------|----------------------|-----|---------|
    789012   | Mary Boe | Computer Information | 3.7 | Senior  |
    123456   | John Doe | Computer Science     | 3.5 | Junior  |
    Here is the code:


    Code:
    package lab13;
    
    import java.util.*;
    import java.io.*;
    public class DataSorting {
       
        public static void main(String[] args) throws FileNotFoundException {
            int[]studentId = new int[25];
            String[]firstName = new String[25];
            String[]lastName = new String[25];
            String[]major = new String[25];
            double[]gpa = new double[25];
            String[]year = new String[25];
    
            Scanner input = new Scanner(new File("studentrecords.txt"));
            int count = 0;
            while(input.hasNextDouble() && count<25)
            {
                count++;
                for(int i=0; i<25; i++)
                {
                    studentId[i] = input.nextInt();
                    firstName[i] = input.next();
                    lastName[i] = input.next();
                    major[i] = input.next();
                    gpa[i] = input.nextDouble();
                    year[i] = input.next();
                    //System.out.printf("%10d %15s %15s %15s %15d %15s %n",studentId[i], firstName[i], lastName[i], major[i], gpa[i], year[i]);
                }
            }
    
            bubbleSort(studentId, firstName, lastName, major, gpa, year);
            System.out.println("after sorting: ");
            System.out.println("    Exam Scores     First Name   Last name");
            for(int i=0; i<studentId.length; i++)
                System.out.printf("%10d %15s %15s %n", studentId[i], firstName[i], lastName[i]);
        }
    
        public static void bubbleSort (int[]dataId, String[]fname, String[]lname, String[]major, double[]gpa, String[]year)
        {
            boolean isSorted;
            for (int R=0; R<dataId.length-1; R++)
            {
                do{
                    isSorted = true;
                    for(int i=1; i<dataId.length-R; i++)
                    {
                        if((lname[i].compareTo(lname[i-1])<0))
                        {
                            int temp1 = dataId[i];
                            String temp2 = fname[i];
                            String temp3 = lname[i];
                            String temp4 = major[i];
                            double temp5 = gpa[i];
                            String temp6 = year[i];
                            dataId[i] = dataId[i-1];
                            fname[i] = fname[i-1];
                            lname[i] = lname[i-1];
                            major[i] = major[i-1];
                            gpa[i] = gpa[i-1];
                            year[i] = year[i-1];
                            dataId[i-1] = temp1;
                            fname[i-1] = temp2;
                            lname[i-1] = temp3;
                            major[i-1] = temp4;
                            gpa[i-1] = temp5;
                            year[i-1] = temp6;
                            isSorted = false;
                        }
                    }
                }
                while(!isSorted);
                }
            }
        }
    Last edited by Frinavale; Dec 2 '09, 08:50 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • mrjohn
    New Member
    • May 2009
    • 31

    #2
    So what's the problem with it?

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      This is not bubble sort.

      Please research the topic of Bubble Sort so that you can have a concrete understanding of how this sort algorithm works before you attempt to implement anything.

      Once you have an understand of how bubble sort works, you should consider making use of the Object Oriented Programming model that Java has to offer.

      Create a class (called "Student" to keep things clear).

      Then Create a List/Collection of Student Objects based on what you read from the file.

      Once you have a list /collection of Student Objects preforming the bubble sort will be a lot easier.

      -Frinny

      Comment

      • elbone2k
        New Member
        • Dec 2009
        • 3

        #4
        the problem with the code is that I cannot get the arrays to store the value/strings read in the file.
        I've been going over the code all day and still don't see a problem with it.
        I keep getting this error:
        Code:
        Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
        I'm sorry, i'm a beginner at this...

        thanks Frinavale for fixing up my earlier post.. new to the site..didn't know i could do that :)

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          It's very hard for me to read your code.
          Have you considered implementing a class like I recommended earlier?

          Comment

          • elbone2k
            New Member
            • Dec 2009
            • 3

            #6
            I do not know how to implement a class?
            Here is what I think is giving me the error but I'm not sure how to fix it.
            Please let me know what you think about it.
            Thank you.

            Code:
            package lab13;
            import java.util.*;
            import java.io.*;
            public class DataSorting {
               
            public static void main(String[] args) throws FileNotFoundException {
            [U]//DECLARING THE ARRAYS THAT WOULD STORE VALUES READ    FROM FILE[/U]
            int[]studentId = new int[25];
            String[]firstName = new String[25];
            String[]lastName = new String[25];
            String[]major = new String[25];
            double[]gpa = new double[25];
            String[]year = new String[25];
            
            [U]//INITIALIZING THE SCANNER TO OPEN FILE[/U]
            Scanner input = new Scanner(new File("studentrecords.txt"));
            int count = 0;
            
            [U]//USING A WHILE LOOP TO STORE THE NEXT READ VARIABLE IN THE FILE[/U]
            while(input.hasNextDouble() && count<25)
            {
            count++;
            for(int i=0; i<25; i++)
            {
            studentId[i] = input.nextInt();[U] //THIS STORES THE STUDENTS ID[/U]
            firstName[i] = input.next(); [U]//THIS STORES THE STUDENTS FIRST NAME[/U]
            lastName[i] = input.next();[U] //THIS STORES THE STUDENTS LAST NAME[/U]
            major[i] = input.next(); [U]//THIS STORES THE STUDENTS MAJOR[/U]
            gpa[i] = input.nextDouble();[B][U]//THIS IS WHERE I THINK THE PROBLEM STARTS, FOR SOME REASON IT WON'T STORE THE NEXT READ VARIABLE WHICH IS SUPPOSED TO BE THE GPA[/U][/B]
            year[i] = input.next();
            System.out.printf("%10d %15s %15s %15s %15d %15s %n",studentId[i], firstName[i], lastName[i], major[i], gpa[i], year[i]);
            }
            }      
            }

            Comment

            • ChipR
              Recognized Expert Top Contributor
              • Jul 2008
              • 1289

              #7
              Have you given us the actual format of the text file?

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Do you have a debugger attached to your main method when you run this? Instead of throwing your file not found exception why don't you try catching all exceptions and print out what they are so you have a better idea?

                There must be more details around your exception in thread main that will lead you to a better answer.

                This probably doesnt matter but does nextDouble return a double or a Double?

                Comment

                Working...