I'm trying to write a program that read a file containing student data in this format
the program is supposed to read the file and organize the data according to the students last name and look like this
Here is the code:
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 |
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); } } }
Comment