In the following code, I have taken in a list of 5 student names, and loading them in an ArrayList of type String.
import java.util.Scanner;
import java.util.ArrayList;
public class QuizAverage
{
public static void main( String[] args ) {
final int NAMELIMIT = 5 ;
final int QUIZLIMIT = 5 ;
ArrayList<String> sNames = new ArrayList<String>();
ArrayList<String> sFamily = new ArrayList<String>();
Scanner in = new Scanner(System.in);
//Load the 5 names of the students in the arraylist
for(int i = 1; i<=NAMELIMIT; i++)
{
String[] input = in.nextLine().split("\\s+");
sNames.add(input[0]);
sFamily.add(input[1]);
}
System.out.println("Name: ");
System.out.println();
for(int i=0; i<NAMELIMIT; i++)
{
System.out.println("Name: " +sNames.get(i) + " " + sFamily.get(i));
}
System.out.println();
}
}
However, now I am trying to add to the code a part that reads in marks for 5 quizes for each student and loads the quiz marks in An ArrayList of type Integer
So I know I need to use
ArrayList<Integer> quizMarks = readArrayList(readQuiz.nextLine());
and then pass it on to this code which takes the quiz marks and weights them out of 15 instead of 100
public static ArrayList<Integer> readArrayList(String input)
{
ArrayList<Integer> quiz = new ArrayList<Integer>();
int i = 1;
while (i <= QUIZLIMIT)
{
if (readQuiz.hasNextInt()) {
quiz.add(readQuiz.nextInt());
i++;
} else {
readQuiz.next(); // toss the next read token
}
}
return quiz;
}
//Computer the average of quiz marks
public static void computerAverage(ArrayList<Integer>quiz)
{
double total = 0 ;
for(Integer value : quiz)
{
total = total + value;
}
total *= MAX_SCORE/100;
System.out.println("Quiz Avg: "+ total / QUIZLIMIT );
}
}
So my current code with the input:
Sally Mae 90 80 45 60 75
Charlotte Tea 60 75 80 90 70
Oliver Cats 55 65 76 90 80
Milo Peet 90 95 85 75 80
Gavin Brown 45 65 75 55 80
Gives the output
Name: Sally Mae
Name: Charlotte Tea
Name: Oliver Cats
Name: Milo Peet
Name: Gavin Brown
when the desired output is
Name: Sally Mae Quiz Avg: 10.5
Name: Charlotte Tea Quiz Avg: 11.25
Name: Oliver Cats Quiz Avg: 10.95
Name: Milo Peet Quiz Avg: 12.75
Name: Gavin Brown Quiz Avg: 9.6
Studentclass that stores a student's first name, family name and list of grades.