1、正则式判断分数
if (s1.matches("-?[0-9]+.?[0-9]*"))
2、定界符分隔(空格 :,)
Scanner input = new Scanner(file).useDelimiter("[ :,]");
3、示例
public class Test1 {
public static void main(String[] args) throws Exception {
File file = new File("file1.txt"); //relative path
Scanner input = new Scanner(file).useDelimiter("[ :,]");
double sum = 0.0;
int count = 0;
while (input.hasNext()) {
String s1 = input.next();
if (s1.matches("-?[0-9]+.?[0-9]*")) {
sum += Double.parseDouble(s1);
System.out.println("is true");
count++;
}
}
input.close();
System.out.println("scores' sum is " + sum + "\nand the average: " + sum / count);
}
}