jane done
jane done

Reputation: 33

Programming Arrays in Java

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

Upvotes: 0

Views: 139

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191711

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 ); 
    }
}

Upvotes: 0

Brian
Brian

Reputation: 3914

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;
}

Upvotes: 1

Related Questions