Java Fundamentals IO Stream (practice)

Source: Internet
Author: User

Practice:

Requirements: There are five students, each student has 3 courses of results,
Enter the above data from the keyboard (including the name, the results of each class),
Input format: such as: zhagnsan,30,40,60 calculated the total,
And the student's information and calculated total score high and low order in the disk file "Stud.txt".
1, describe the student object.
2, defines a tool class that can manipulate student objects.
Thought:
1, by getting the keyboard input a row of data, and the information in the row is taken out to encapsulate the student object.
2, because students have a lot, then you need to store, use to the collection. Because the student's total score is to be sorted.
So you can use TreeSet.
3, writes the information of the collection to a file.

Importjava.io.*;ImportJava.util.*;class Studenttest { Public Static void Main(string[] args)throwsIOException {set<student> students=studentinfoutil.getstudents ();    Studentinfoutil.write2file (students); }}class Student implements comparable{ PublicString name; Public intyu,shu,ying; Public intZong Student (String name,intYuintShuintYing) { This. name=name; This. Yu=yu; This. Shu=shu; This. ying=ying; This. zong=yu+shu+ying; } Public int CompareTo(Object obj) {if(! (objinstanceofStudent))Throw NewRuntimeException ("not a student."); Student stu= (Student) obj;if( This. Zong<stu.zong)return 1;Else if( This. Zong==stu.zong)return  This. Name.compareto (Stu.name);return-1; }}class studentinfoutil{ Public StaticSet<student>getstudents()throwsIOException {BufferedReader bur=NewBufferedReader (NewInputStreamReader (system.in)); String line=NULL; Set<student> students=NewTreeset<student> (); while((Line=bur.readline ())! =NULL)        {if(Line.equals ("Over")) Break; String[] Info=line.split (","); Student stu=NewStudent (info[0],integer.parseint (info[1]), Integer.parseint (info[2]), Integer.parseint (info[3]));        Students.add (Stu); } bur.close ();returnStudents } Public Static void Write2file(set<student> students)throwsIOException {BufferedWriter buw=NewBufferedWriter (NewFileWriter ("Stuinfo.txt")); for(Student stu:students) {Buw.write (stu.name+","+stu.zong);            Buw.newline ();        Buw.flush ();    } buw.close (); }}

Java Fundamentals IO Stream (practice)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.