要求按行读取成绩单。并在该行的后面加上该同学的总成绩。然后将该行写入到一个名字为scoreAnalysis.txt的文件中。
mport java.io.*;
import java.util.*;
class Fenxi {
public static double getTotalscore(String s) {
Scanner sc=new Scanner(s);
sc.useDelimiter("[^0123456789.]+");
double tscore=0;
while(sc.hasNext()) {
try {
double score=sc.nextDouble();
tscore += score;
}catch(InputMismatchException e) {
String t=sc.next();
}
}
return tscore;
}
}
public class AnalysisResult {
public static void main(String[] args) {
File fr=new File("score.txt");
File fw=new File("scoreAnalysis.txt");
try {
Writer out=new FileWriter(fw);
BufferedWriter bfw=new BufferedWriter(out);
Reader in=new FileReader(fr);
BufferedReader bfr=new BufferedReader(in);
String str=null;
while((str=bfr.readLine())!=null) {
double totals=Fenxi.getTotalscore(str);
str=str+" zongFen:"+totals;
System.out.println(str);
bfw.write(str);
bfw.nextLine();
}
bfr.close();
bfw.close();
}catch(IOException e) {
}
}
}