Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this Arraylist string program for java. I cant get the \"Studen

ID: 3698938 • Letter: I

Question

I need help with this Arraylist string program for java. I cant get the "Students in Class" to not print on repeat. It needs to say, "Students in Class: " and then 10. bc the nNumberStudents is an accessor. This is what i Have:

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");
System.out.println("");
  
  
  
generateStuList(stuList);
  

for (int nIndex = 0; nIndex < stuList.size(); nIndex++) {
printStudentsQuiz(stuList.get(nIndex));
  
}
} //End of main method
public static void printStudentsQuiz(StudentsQuiz myStudentsQuiz){
System.out.println("Students in class: " + myStudentsQuiz.getNumberStudents());

  
System.out.println("");
  
System.out.println("Student Name: "+ myStudentsQuiz.getFirstName() + " " + myStudentsQuiz.sLastName.toUpperCase());
System.out.println("Quiz Average: "+ myStudentsQuiz.calculateQuizAverage());
System.out.println("");
  
}//End printStudentsQuiz method
public static void generateStuList(ArrayList<StudentsQuiz > list) throws FileNotFoundException {
String sFileName = "Students.txt";
String sInputLine = "";
File fileToOpen = new File(sFileName);
Scanner inputFile = new Scanner(fileToOpen);
String[] saTokens = null;
  
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
}//End generateStuList method
}//End of StudentsQuiz2 class

Explanation / Answer

Answer :

You can always declare that print statement outside of the 'for' loop to not print that staement on repeat. Like this :

public static void main(String[] args) throws FileNotFoundException {

ArrayList<StudentsQuiz> stuList = new ArrayList<StudentsQuiz>();

System.out.println("STUDENTS ROOSTER AND QUIZ AVERAGES");

System.out.println("");

generateStuList(stuList);

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

for (int nIndex = 0; nIndex < stuList.size(); nIndex++) {

printStudentsQuiz(stuList.get(nIndex)); // printing the names and marks of each student repeatedly

}

} // End of main method

public static void printStudentsQuiz(StudentsQuiz myStudentsQuiz) {

System.out.println("");

System.out.println("Student Name: "+ myStudentsQuiz.getFirstName() + " " + myStudentsQuiz.sLastName.toUpperCase());

System.out.println("Quiz Average: "+ myStudentsQuiz.calculateQuizAverage());

System.out.println("");

} // End printStudentsQuiz method