AnalysisResult.java
import java.io.*;
import java.util.*;
public class AnalysisResult {
public static void main(String args[]) {
File fRead = new File("F:\\JJJ\\score.txt"); //文件路径
File fWrite = new File("F:\\JJJ\\socreAnalysis.txt");//文件路径
try{ Writer out = new FileWriter(fWrite);//以尾加方式创建指向文件fWrite的out流
BufferedWriter bufferWrite = new BufferedWriter(out);//创建指向out的bufferWrite流
Reader in = new FileReader(fRead);//创建指向文件fRead的in流
BufferedReader bufferRead =new BufferedReader(in);//创建指向in的bufferRead流
String str = null;
while((str=bufferRead.readLine())!=null) {
double totalScore=Fenxi.getTotalScore(str);
str = str+" 总成绩:"+totalScore;
System.out.println(str);
bufferWrite.write(str);
bufferWrite.newLine();
}
bufferRead.close();
bufferWrite.close();
}
catch(IOException e) {
System.out.println(e.toString());
}
}
}
注:记得创建txt文件以及修改文件路径。
Fenxi.java
import java.util.*;
public class Fenxi {
public static double getTotalScore(String s) {
String regex = "[^0123456789.]"; //匹配非数字的正则表达识
String digitMess = s.replaceAll(regex, "*");
StringTokenizer fenxi = new StringTokenizer(digitMess, "*");
double totalScore = 0;
while (fenxi.hasMoreTokens()) {
double score = Double.parseDouble(fenxi.nextToken());
totalScore = totalScore + score;
}
return totalScore;
}
}
运行结果: