In the following code I am trying to read in the 5 test value scores and calculate the average for each row. I have used an ArrayList of type Integer to read in these values inputed. So I want to read past the first and second columns and then compute the average of the next 5 columns
import java.util.ArrayList;
import java.util.Scanner;
public class heya
{
public static void main( String[] args )
{
ArrayList<Integer> quizMarks = readArrayList();
computerAverage(quizMarks);
}
// Load quiz marks
public static ArrayList<Integer> readArrayList()
{
final int QUIZLIMIT = 5 ;
ArrayList<Integer> quiz = new ArrayList<Integer>();
Scanner readQuiz = new Scanner(System.in);
for(int i = 1; i<=QUIZLIMIT; i++)
{
quiz.add(readQuiz.nextInt());
}
return quiz;
}
//Computer the average of quiz marks
public static void computerAverage(ArrayList<Integer>quiz)
{
final int QUIZLIMIT = 5;
int total = 0 ;
for(Integer value : quiz)
{
total = total + value;
}
System.out.println("Quiz Avg: "+ (total/QUIZLIMIT));
}
}
With the following 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
Inputed into my code gives the error:
CompileRunTest: throwable = java.util.InputMismatchException
java.util.InputMismatchException
When the desired output is:
Quiz Avg: 10.5
Quiz Avg: 11.25
Quiz Avg: 10.95
Quiz Avg: 12.75
Quiz Avg: 9.6
readArrayList()method doesn't match the input. Your code only expects 5 numbers, no names.readQuiz.nextInt()handle grabbing the 5 integers?readQuiz.readLine(), thensplit()that, then add the necessary numbers to the arraylist