Main function class:
Package Cn.io;import Java.io.file;import Java.io.ioexception;import java.util.collection;import Java.util.collections;import Java.util.comparator;import Java.util.set;public class Iosort {/** * @param args * @throws I Oexception *//* * Example: keyboard input information of more than one student: format: name, Math score, language score, English score. * The students ' information is arranged in the file according to the total score from high to low. * Idea: * 1, using keyboard input technology. * 2, the operation of student information, a lot of information, need to encapsulate information into student objects. * 3, the total score from high to low, need to sort, need to sort the scores of the students ' objects. Multiple student objects need to be stored in containers. * 4, the information of the student object in the container is written to the file. * Container: TreeSet * * Use the tool class to manipulate student information */public static void Main (string[] args) throws IOException {comparator<student> comp = Collections.reverseorder (); set<student> set = getinfotool.getstudents (comp); File DestFile = new file ("E:\\3.txt"); Getinfotool.writrfile (set,destfile);/* Input xiao,13,12,13ss,13,24,24de,254,4,2over*/ }}
Tool classes for manipulating student information:
<pre class= "java" name= "code" >package cn.io;import java.io.bufferedreader;import java.io.BufferedWriter; Import Java.io.file;import java.io.filewriter;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.util.comparator;import Java.util.set;import Java.util.treeset;public class Getinfotool {public static Set<Stu Dent> getstudents () throws IOException {return getstudents (null);} public static set<student> getstudents (comparator<student> comp) throws IOException {BufferedReader buf = NE W BufferedReader (New InputStreamReader (system.in)); Set<student> set = null; if (comp!=null) {set=new treeset<student> (comp);} else {set=new treeset<student> ();} String line = Null;while ((Line=buf.readline ())!=null) {if (Line.contains (' over ')) {break;} String[] STRs =line.split (","); Student stu = new Student (Strs[0], Integer.parseint (strs[1]), Integer.parseint (Strs[2]), Integer.parseint (strs[3])); Set.add (stu);} return set;} public static VOID writrfile (set<student> Set, File destfile) {BufferedWriter BFW = null;try {bfw = new BufferedWriter (New FileWrite R (DestFile)); for (Student Stu:set) {bfw.write (Stu.getname () + "\ T" +stu.getsum ()); Bfw.newline (); Bfw.flush ();}} catch (IOException e) {e.printstacktrace ();} finally {if (bfw!=null) {try {bfw.close ();} catch (IOException e) {e.printstacktrace ();}}}} }
Student Information Category:
Package Cn.io;public class Student implements comparable<student> {public int getsum () {return sum;} public void setsum (int sum) {this.sum = sum;} private String name; private int grade1; private int grade2; private int grade3; private int sum;public Student (String name, int grade1, int grade2, int grade3) {super (); This.setname (name); This.setgrade 1 (grade1); This.setgrade2 (Grade2); This.setgrade3 (grade3); this.sum=grade1+grade2+grade3;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int GetGrade1 () {return grade1;} public void setGrade1 (int grade1) {this.grade1 = Grade1;} public int getGrade3 () {return grade3;} public void setGrade3 (int grade3) {this.grade3 = Grade3;} public int GetGrade2 () {return grade2;} public void SetGrade2 (int grade2) {this.grade2 = Grade2;} @Overridepublic int compareTo (Student o) {//TODO auto-generated method Stubint temp = this.sum-o.sum; return temp = = 0?th Is.name.compareTo (o.name): temp; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Keyboard input information for multiple students: format: Name, Math score, language score, English score, according to the total score from high to low the information of the students are arranged into the file