Write a program that read data.txt as input file. Then print student's name who scored 60 and below. Count and display students who scored A
Ahmad
90
A
Ali
80
B
Siti
70
B
Abu
47
E
Aminah
98
A
Noraini
40
E
Anuar
50
C
Zaid
52
C
import java.util.*;
import java.io.File;
class App {
@SuppressWarnings("resource")
public static void main(String args[]) {
int student60 = 0;
int studentA = 0;
String namesWhoScored60 = "";
String namesWhoScoredA = "";
try {
final String FILE_NAME = "data.txt";
Scanner input = new Scanner(FILE_NAME);
File file = new File(input.nextLine());
if (file.exists()) {
input = new Scanner(file);
while (input.hasNextLine()) {
String name = input.nextLine();
String scoreStr = input.nextLine();
int score = Integer.parseInt(scoreStr);
String grade = input.nextLine();
if (score <= 60) {
student60++;
namesWhoScored60 += name + "\n";
}
if (grade.compareToIgnoreCase("A") == 0) {
studentA++;
namesWhoScoredA += name + "\n";
}
}
input.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Student's names who scored 60 and below:\n" + namesWhoScored60);
System.out.println("Total students who scored 60 and below: " + student60);
System.out.println("\nStudent's names who scored A:\n" + namesWhoScoredA);
System.out.println("Total students who scored A: " + studentA);
}
}
Comments
Leave a comment