Question 161
Multiple choice
Using Text Files
Consider a class
What is printed?
Student with constructor Student(String name, int grade) and methods getName() returning the name and getGrade() returning the grade. A text file contains a header row followed by data:Name,GradeAlice,95Bob,87Carol,91What is printed?
sc.nextLine(); // skip header
ArrayList<Student> list = new ArrayList<Student>();
while (sc.hasNext()) {
String[] parts = sc.nextLine().split(",");
list.add(new Student(parts[0], Integer.parseInt(parts[1])));
}
sc.close();
System.out.println(list.size() + " " + list.get(0).getName());
Pick an answer, then check it. The solution opens in a window.
BCorrect answer
Answer images

