Ava writes something to read the data in TXT and to calculate the average and the total value to be sorted by the total value.
Java writes something to read the data in TXT and to calculate the average and the total value to be sorted by the total value.
For example: To calculate the contents of the A.txt document can be as follows:
Study number name Chinese mathematics English average value of the order
1 KFC 90 98 97
2 Classic 98 97 92
3 Kendall's 93 92 97
Import java.io.*;import java.io.file;import java.util.arraylist;import java.util.iterator;import java.util.List; Import Java.util.treeset;public class Filenumberdemo {public static void main (string[] args) throws ioexception{ File File = new file ("D:\\a.txt"); if (!file.exists ()) throw new RuntimeException ("file does not exist!) "); BufferedReader br = new BufferedReader (new FileReader (file)); BufferedWriter bw = new BufferedWriter (New FileWriter ("D:\\b.txt")); String str = NULL; list<string> list = new arraylist<string> (); while ((Str=br.readline ())!=null) {list.add (str); } string[] arr = new string[list.size ()]; treeset<student> tr = new treeset<student> (); arr = List.toarray (arr); for (int i = 0; i < arr.length; i++) {string[] line = arr[i].split ("+"); for (int j = 0; J < Line.length; J + +) {if (i = = 0) bw.write(line[j]+ "\ t"); else{Tr.add (New Student (Integer.parseint (line[0]), Line[1],integer.parseint (Line[2]), Integer.parseint (Line[3]), Integer.parseint (Line[4]), (Int Eger.parseint (line[2]) +integer.parseint (line[3]) +integer.parseint (line[4])/3), (Integer.parsein T (Line[2]) +integer.parseint (line[3]) +integer.parseint (line[4]))); }} System.out.println (); } bw.write ("\ r \ n"); int num = 1; for (iterator<student> it = Tr.iterator (); It.hasnext ();) {bw.write (It.next (). toString ()); Bw.write ("\ T" + (num++) + "\ r \ n"); } bw.close (); }}class Student implements comparable<student>{private int num; private String name; private int yw; private int SX; private int 中文版; private int PJ; private int sum; Public Student (int num,stringName,int yw,int sx,int english,int pj, int sum) {this.num = num; THIS.name = name; This.yw = yw; THIS.SX = SX; This.english = 中文版; THIS.PJ = PJ; This.sum = sum; } public int CompareTo (Student stu) {int num = integer.valueof (stu.sum). CompareTo (Integer.valueof (this.sum)); if (num = = 0) return integer.valueof (Stu.num). CompareTo (Integer.valueof (this.num)); return num; The public String toString () {return num+ "\ t" +name+ "\ T" +yw+ "\ T" +sx+ "\ T" +english+ "\ T" +pj+ "\ T" +sum; }}
Through the above procedure, the results calculated are as follows:
Study number name Chinese mathematics English average value of the order
2 Classic 98 97 92 225 287 1
1 KFC 90 98 97 220 285 2
3 Kendall's 93 92 97 217 282 3
Java writes something to read the data in TXT and to calculate the average and the total value to be sorted by the total value.