//readStu.java --- you can use this example import java.io.*; import java.util.*; class readStu { PrintStream cout=null; BufferedReader cin=null; Student y[ ]; // all students in y[ ] public static final String fileName="studat.txt"; public static void main(String xxx[ ]) { readStu gg = new readStu( ); gg.prepareIO( ); gg.main( ); } public readStu( ) { } void prepareIO( ) { try{ cout = System.out; cin = new BufferedReader( new InputStreamReader(System.in) ); }catch(Exception e) {;} } //////// int main( ) { int nRec = readFile(fileName); cout.printf("Total %d students in file %s\n", nRec, fileName); printSome(y); Arrays.sort(y); cout.println(" ==After sort..."); printSome(y); Arrays.sort(y, Collections.reverseOrder( ) ); cout.println("===After sort with reverseOrder( )..."); printSome(y); return 0; }//int main( void printSome(Student a[ ]) { int i=0; for(i=0; i<3; ++i) printOne(a[i], 0); cout.println("..."); ////// int last = a.length; for(i= last-3; i < last; ++i) printOne(a[i], 1); //with RANK } ///////// int readFile(String fn) { BufferedReader br=null; int nR = 0; nR = countRec(fn); y = new Student[nR]; // creat array try { // open file FileReader fr = new FileReader(fn); br = new BufferedReader(fr); }catch (Exception e) {;;} for(int i=0; i < nR; ++i) { y[i] = new Student( ); String s=null; try{ s = br.readLine( ); }catch (Exception e) {;;} putData(y[i], s); } return nR; }//readFile int countRec(String fn) { // count # of records int n = 0; try { FileReader fr = new FileReader(fn); BufferedReader br = new BufferedReader(fr); String s = br.readLine( ); while(s != null) { // not EOF ++n; s = br.readLine( ); }//while(s try{if(br!=null)br.close( );}catch(Exception e){;} }catch (Exception e) {;;} return n; }//countRec( void putData(Student gg, String s) { StringTokenizer stk = new StringTokenizer(s," \t"); String tmps = stk.nextToken( ); gg.sid = 0; try{ gg.sid = Integer.parseInt(tmps); }catch(Exception e){} gg.name = new String(stk.nextToken( ) ); gg.hwk = 0; tmps = stk.nextToken( ); try{ gg.hwk = Integer.parseInt(tmps); }catch(Exception e){} gg.mid = 0; tmps = stk.nextToken( ); try{ gg.mid = Integer.parseInt(tmps); }catch(Exception e){} //...todo: quiz, fin gg.total = 0.25 * gg.hwk +0.3*gg.mid+0.1*gg.quiz+0.35*gg.fin; gg.rank = 0; }//putData( void printOne(Student gg, int flag) { // flag for Rank cout.print(" " + gg.sid +" "); cout.printf("%12s ", gg.name); cout.printf("%3d %3d %3d %3d ", gg.hwk, gg.mid, gg.quiz, gg.fin); cout.printf("%6.2f ", gg.total); if(flag!=0) cout.printf("%5d", gg.rank); cout.println( ); } }//readStu class Student implements Comparable { public int sid; public String name; public int hwk, mid, quiz, fin; public double total; public int rank; public int compareTo(Student bb) { return sid - bb.sid; // Ascending on sid }//compareTo( }//Student /******************** C:\jtest\struct>javac readStu.java C:\jtest\struct>java readStu Total 81493 students in file studat.txt 9834527 林ZQj 17 60 0 0 22.25 9840229 簡PUa 57 7 0 0 16.35 9826528 王BLa 95 70 0 0 44.75 ... 9898491 魏NGk 24 48 0 0 20.40 0 9898492 錢GTe 34 48 0 0 22.90 0 9898493 趙GGg 29 60 0 0 25.25 0 ==After sort... 9817001 張CZt 64 35 0 0 26.50 9817002 張PKs 91 58 0 0 40.15 9817003 魏OBa 9 16 0 0 7.05 ... 9898491 魏NGk 24 48 0 0 20.40 0 9898492 錢GTe 34 48 0 0 22.90 0 9898493 趙GGg 29 60 0 0 25.25 0 ===After sort with reverseOrder( )... 9898493 趙GGg 29 60 0 0 25.25 9898492 錢GTe 34 48 0 0 22.90 9898491 魏NGk 24 48 0 0 20.40 ... 9817003 魏OBa 9 16 0 0 7.05 0 9817002 張PKs 91 58 0 0 40.15 0 9817001 張CZt 64 35 0 0 26.50 0 C:\jtest\struct> ******************************************************/