[英]Create a file with students and view the information from the students that have more average grade from 5 and the average grade from each student
//Create the file Alumns.txt where we have a group of students with their last names, names and average grade, separed by “,” and view the information from the students that have more average grade from 5 and the average grade from each student.
//We have the file Alumns.txt with the content:
//雷莫拉米雷斯,諾埃利亞,6,希門尼斯桑托斯,卡洛斯,4,佩雷斯戈麥斯,費爾南多,7,漢密爾頓布萊恩,泰勒,3,古鐵雷斯門多薩,何塞,8,加西亞雷耶斯,瑪麗亞,6,馬爾坎特門德斯,盧娜,3 ,德爾加多·門德斯,天使,10。
public class Main {
public static void main(String[] args) {
File fichero = null;
FileReader fr = null;
Scanner sc = null;
double [] notas = new double [6];
文件校友.txt
fichero = new File ("Alumnos.txt");
try {
BufferedReader br = new BufferedReader(fr);
String texto2 = br.readLine();
fr = new FileReader(fichero);
sc = new Scanner(fichero);
String texto = sc.nextLine();
StringTokenizer tokens = new StringTokenizer(texto2, ",");
//我嘗試過使用 Scanner 和 BufferedReader 並且“notas”(數字)每個都是 3 個標記。
for (int i = 0; i < notas.length; i++) {
tokens.nextToken();
tokens.nextToken();
notas[i] = (double)Double.parseDouble(tokens.nextToken());
System.out.println("Nota: "+notas[i]);
if (notas [i]>5) {
System.out.println("Pass the grade 5: "+tokens.nextToken());
}
}
//I catch the exceptions here:
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
假設我們有這個文件grades.csv
Remo Ramirez,Noelia,6
Jimenez Santos,Carlos,4
Perez Gomez,Fernando,7
Hamilton Bryan,Taylor,3
Gutierrez Mendoza,Jose,8
Garcia Reyes,Maria,6
Marchante Mendez,Luna,3
Delgado Mendez,Angel,10
我們可以編寫這個方法來解析它並只打印平均成績高於 5 的學生。
public static void printGradesBetterThanFive() throws IOException {
//Create a file object, path to the file might differ in your case.
File grades = new File("grades.csv");
if (grades.isFile()) {
//Create a BufferedReader, taking in a FileReader with our File as an argument
BufferedReader csvReader = new BufferedReader(new FileReader(grades));
//Parse through each line in the file until we reach the end of it
String line;
while ((line = csvReader.readLine()) != null) {
//Create an array containing values from each line, separated by commas.
String[] lineData = line.split(",");
//If the last value (the grade) is more than 5, print it.
if (Integer.parseInt(lineData[2]) > 5) {
System.out.println(Arrays.toString(lineData));
}
}
csvReader.close();
}
}
你的問題有點含糊,但我希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.