个人反思
既然是假期那就先把个人反思放在前面~
这两天讲真也是很日常颓废了,说好的赶ddl呢?
自从腿受伤之后便有理由宅在寝室,生活节奏陡然变慢
而且不理各种ddl自顾自地玩,更难得地打了一会游戏
估计接下来ddl一多就更没有时间了
珍惜当下 思考未来
个人所获
对书签的整理
为了方便pre试玩了下爬虫
Java作业处理
作业描述
有一个文本文件,内容为学生成绩,每行一个学生,每行格式为 [姓名] [分数],请使用java读取该文件,并按照成绩排序输出。
完成情况
- main.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class main {
public static void main (String [] args){
File data = new File("Read'n'sort\\src\\main\\data.txt");
BufferedReader reader = null;
ArrayList <info> thelist = new ArrayList <info>();
try{
InputStreamReader isr = new InputStreamReader(new FileInputStream(data), "GBK");
reader = new BufferedReader(isr);
String tempString = null;
int line = 1;
while((tempString = reader.readLine())!=null){
//System.out.println("line"+line+": " + tempString);
int n = 0;
char tempchar;
String tempName = "";
String tempScore = "";
try{
while(true) {
tempchar = tempString.charAt(n);
n++;
if(tempchar == ' ') break;
tempName += tempchar;
}
}
catch(StringIndexOutOfBoundsException e){}
try{
while(true) {
tempchar = tempString.charAt(n);
tempScore += tempchar;
n++;
}
}
catch(StringIndexOutOfBoundsException e){}
finally{
int temp = 0;
try{
temp = Integer.parseInt(tempScore);
}
catch(NumberFormatException e){
}
info now = new info();
now.setting(tempName,temp);
thelist.add(now);
}
line++;
}
System.out.print("sort by grade:\n");
Collections.sort(thelist, new SortByGrade());
for(int i = 0;i < thelist.size(); i ++){
thelist.get(i).print();
}
} catch(IOException e){
e.printStackTrace();
} finally{
if(reader != null){
try{
reader.close();
} catch(IOException e1){}
}
}
}
}
- info.java
public class info {
info(){
name = null;
score = 0;
}
void setting(String names , int scores){
name = names;
score = scores;
}
int getGrade(){
return score;
}
String getName(){
return name;
}
void print(){
System.out.println(name+": "+score);
}
String name;
int score;
}
- SortByGrade.java
import java.util.Comparator;
class SortByGrade implements Comparator {
public int compare(Object o1, Object o2) {
info s1 = (info) o1;
info s2 = (info) o2;
if (s1.getGrade() < s2.getGrade())
return 1;
return -1;
}
}
网友评论