0

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
4
  • 2
    Your readArrayList() method doesn't match the input. Your code only expects 5 numbers, no names. Commented Jul 12, 2016 at 19:56
  • @sstan Shouldn't readQuiz.nextInt() handle grabbing the 5 integers? Commented Jul 12, 2016 at 19:57
  • okay how do i go about fixing that? Commented Jul 12, 2016 at 19:57
  • 3
    For a simple solution, I would use readQuiz.readLine(), then split() that, then add the necessary numbers to the arraylist Commented Jul 12, 2016 at 19:59

2 Answers 2

1

Use next() to parse the names:

public static ArrayList<Integer> readArrayList()
{
    final int QUIZLIMIT = 5 ;
    ArrayList<Integer> quiz = new ArrayList<Integer>();
    Scanner readQuiz = new Scanner(System.in);
    //This just throws away the names; 
    //You may wish to use a Quiz class that includes the name and the ArrayList of Integers.
    readQuiz.next();
    readQuiz.next();
    for(int i = 1; i<=QUIZLIMIT; i++)
    {
        quiz.add(readQuiz.nextInt());
    }
    return quiz;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Bohemian Good catch; I just added the necessary second next()
this just gives me one single output. however i need a output that caculate the average of each line, so i need 5 outputs
@janedone I can't speak to that point; I just edited your code to avoid the exception. If it's not working at that point, you need to rethink your algorithm.
0

I am trying to read in the 5 test value scores and calculate the average for each row

First problem - You are only looking at the first row, not all of them. To fix this, use a while loop to read all available lines.

I want to read past the first and second columns and then compute the average of the next 5 columns

There are several ways to approach this, but the below solution checks if there are integers to scan (5, to be exact) and otherwise consumes and ignores the input. This way, you can have a line that looks like some 30 text 59 50 thing 20 21 and it should still work. Careful about the infinite loop if you don't have 5 integers on a line, though.

import java.util.ArrayList;
import java.util.Scanner;
public class heya  
{
    public static final int QUIZLIMIT = 5;
    public static final double MAX_SCORE = 15;
    public static Scanner readQuiz;

    public static void main( String[] args ) 
    {
        readQuiz = new Scanner(System.in);

        while (readQuiz.hasNextLine()) {
            ArrayList<Integer> quizMarks = readArrayList(readQuiz.nextLine());
            computerAverage(quizMarks);
        }
    }
    // Load quiz marks
    public static ArrayList<Integer> readArrayList(String input)
    {
        ArrayList<Integer> quiz = new ArrayList<Integer>();
        Scanner readQuiz = new Scanner(input);
        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 ); 
    }
}

6 Comments

awesome that works now quite well, i just need to now change the average to one that is out of 15 instead of 100
how would i go about changing that average to out of 15?
i would have to change it to a double since the outputs should be 10.5 11.25 10.95 12.75 9.6
Yes, the total should be double. And I don't understand how that first output is 10.5 when all the numbers of 90 80 45 60 75 are much larger than 10.... So that definitely is not the average.
yes i needed to change the weight of the average out of 15 instead of 100
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.