Hello, need help with this arraylist. I got the program correct but I cant figur
ID: 3699299 • Letter: H
Question
Hello, need help with this arraylist. I got the program correct but I cant figure out how to prompt user for name and print the name that gets prompt. I know its a boolean and uses the for loop. confused. heres the file i have. and the example that needs to be the output. my file shows everything cept the last line.
import java.util. Scanner;
import java.io.*;
import java.util.ArrayList;
public class StudentsQuiz2 {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<StudentsQuiz> stuList = new ArrayList<StudentsQuiz>();
System.out.println("STUDENTS ROOSTER AND QUIZ AVERAGES");//Print title of rooster and quizzes
System.out.println("");//Print blank space
generateStuList(stuList);//Generate the array list
StudentsQuiz stQuiz = stuList.get(0); // Storing the returned object by stuList.get(0) in stQuiz object of type StudentsQuiz as initially nIndex was zero when this line was getting printed repeatedly
System.out.println("Students in Class: " + stQuiz.getNumberStudents());// Printing count of students just once
System.out.println("");//Print blank space
for (int nIndex = 0; nIndex < stuList.size(); nIndex++) {
printStudentsQuiz(stuList.get(nIndex));//Initilize the for loop for list
}//End of for loop
} //End of main method
public static void printStudentsQuiz(StudentsQuiz myStudentsQuiz){
System.out.println("Student Name: "+ myStudentsQuiz.getFirstName() + " " + myStudentsQuiz.sLastName.toUpperCase());//Print out first and last name of students from list
System.out.println("Quiz Average: "+ myStudentsQuiz.calculateQuizAverage());//Print out quiz averages for each student
System.out.println("");//Print blank space
}//End printStudentsQuiz method
public static void generateStuList(ArrayList<StudentsQuiz > list) throws FileNotFoundException {
//Read input from file
String sFileName = "Students.txt";
String sInputLine = "";
File fileToOpen = new File(sFileName); //Creating wrapper class for the file name and its directory path
Scanner inputFile = new Scanner(fileToOpen);//Creating scanner object for reading inputs
String[] saTokens = null;
//Initialize the while loop
while (inputFile.hasNext()){
sInputLine = inputFile.nextLine();
saTokens = sInputLine.split("-");
StudentsQuiz stu = new StudentsQuiz();
stu.setFirstName(saTokens[0]);
stu.setLastName(saTokens[1]);
stu.setQuiz1(Integer.parseInt(saTokens[2]));
stu.setQuiz2(Integer.parseInt(saTokens[3]));
stu.setQuiz3(Integer.parseInt(saTokens[4]));
list.add(stu);
}//End while loop and read all lines from the file
}//End generateStuList method
}//End of StudentsQuiz2 class
Prompt the user to input a student name. Try to find this student in the ArrayList. Once found, print the student’s name and quiz average to the screen. If not found, print an error message. Please refer to the Sample Output – BONUS file for formatting details. You do not need special syntax to do this – you will have to think logically about how to solve this problem.
Scenario 1: User searches for a student who exists. STUDENT ROSTER AND QUIZ AVERAGES
Please enter the last name of a specific student: buchwald Student Name: Drew BUCHWALD
Quiz average: 73.0
1
Scenario 2: User searches for a student who does not exist. STUDENT ROSTER AND QUIZ AVERAGES
Students in class: 10
Student Name: Leann ROSENBERRY
Please enter the last name of a specific student: Smith No student with name Smith found.
Explanation / Answer
The relevant code for prompting the user for name and displaying its details
is as follows:
In main function we have
for (int nIndex = 0; nIndex < stuList.size(); nIndex++) {
printStudentsQuiz(stuList.get(nIndex));//Initilize the for loop for list
}//End of for loop
for printing the details of the students
After this we can have a while loop as follows:
Scanner sc = new Scanner(System.in);
while (true){
System.out.print("Please enter the last name of a specific student:");
String last = sc.nextLine();
int found = 0;
for (int i = 0; i<stuList.size(); i++){
if (stuList.get(i).getLastName().equals(last.toUpperCase())){
found = 1;
System.out.println("Student Name:" + stuList.get(i).getFirstName() + stuList.get(i).getLastName().toUpperCase())
System.out.println("Quiz Average: "+ stuList.get(i).calculateQuizAverage());
}
}
if (found == 0){
System.out.println("No student with name " + last + " is found.");
}
System.out.println("Do you wnat to continue? (y/n) :");
String ch = sc.nextLine();
if (ch.charAt(0) == 'n')
break;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.