Read the number (n) of students and units (k) from the user and read the (k) s for (n) students from a file specified by the user. As an example, if the user gives n=2, k=3, the file will look something like, (please use JAVA language) Tian 68.7 89.5 78.2Seung 87.1 71.0 80.0
write a summary of the s in an output file, like, Student Name Average_________________________Tian 78.8Seung 79.3多谢。(这是一个ARRAY)
write a summary of the s in an output file, like, Student Name Average_________________________Tian 78.8Seung 79.3多谢。(这是一个ARRAY)
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class ArrayFile { public static void main(String[] args) {
BufferedWriter bw = null;
BufferedReader br = null;
try {
bw = new BufferedWriter(new FileWriter("D:/a.TXT"));
br = new BufferedReader(new FileReader("d:/res.txt"));
} catch (IOException e) {
e.printStackTrace();
}
String str = null;
String name = null;
float num = 0;
try {
bw.write("Student\t\tAverage");
bw.newLine();
bw.write("----------------------");
bw.newLine();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
while((str = br.readLine()) != null) {
String[] buf = str.split(" ");
int index = 0;
for(int i = 0 ; i < buf.length ; i ++) {
if(buf[i].matches("[a-zA-Z]+")) {
name = buf[i];
}
if(buf[i].matches("^[0-9]*.[0-9]*")) {
index ++;
num += Float.parseFloat(buf[i]);
}
}
float avg = num/index;
name = name + "\t\t" + avg;
bw.write(name);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bw != null) {
bw.flush();
bw.close();
bw = null;
}
if(br != null) {
br.close();
br = null;
}
} catch(IOException ex) {
ex.printStackTrace();
}
}
}}
初学正则表达式,拿这个题做个实验,不知道是不是这么要求的!
我qq82668558,有机会大家讨论啊
过于懒了,没写注释,不好意思