jane done jane done - 4 months ago 8
Java Question

Combining Arrays in Java

In the following code I needed to read in a list of five student names and marks for five quizzes for each student, which loads the names in an ArrayList of type String and the quiz marks in an ArrayList of type Integer. I have broken this problem down by the two different ArrayLists, and I am looking to combine them but not sure.

The following code reads five students names and loads the names in an ArrayList of type String

import java.util.ArrayList;
public class QuizAveragee
{
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 five 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]);
}
for(int i=0; i<NAMELIMIT; i++)
{
System.out.println("Name: " + sNames.get(i) + " " + sFamily.get(i));
}
System.out.println();
}
}


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


It generates:

Name: Sally Mae
Name: Charlotte Tea
Name: Oliver Cats
Name: Milo Peet
Name: Gavin Brown


I then needed to make a part of the program that will read in five quizzes for each student and load the quiz marks in an ArrayList of type integer. This is what I have generated for this part.

import java.util.ArrayList;
import java.util.Scanner;
public class heya
{
public static final int QUIZLIMIT = 5;
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;
}

// Compute the average of quiz marks
public static void computerAverage(ArrayList<Integer>quiz)
{
int total = 0 ;
for(Integer value : quiz)
{
total = total + value;
}
System.out.println("Quiz Avg: "+ (total/QUIZLIMIT));
}
}


which gives the output:

Quiz Avg: 70
Quiz Avg: 75
Quiz Avg: 73
Quiz Avg: 85
Quiz Avg: 64


However, I need to combine these programs which I am not quite sure how to do. The given 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


should give:

Name: Sally Mae Quiz Avg: 70
Name: Charlotte Tea Quiz Avg: 75
Name: Oliver Cats Quiz Avg: 73
Name: Milo Peet Quiz Avg: 85
Name: Gavin Brown Quiz Avg: 64

Answer

This can be accomplished much more simply with a single loop, assuming you know the data will be in the right form (a first and last name followed by 5 grades) and you don't need to store the names or grades for later use.

public class QuizAveragee {
  private static final int NAMELIMIT = 5;

  public static void main(String[] args) {
    //these lines are not needed but OP asked for the values to be stored in arrays
    ArrayList<String> names = new ArrayList<>();
    ArrayList<Double> averages = new ArrayList<>();
    Scanner in = new Scanner(System.in);

    for (int i = 0; i < NAMELIMIT; i++) {
        String line = in.nextLine();
        String[] words = line.split(" ");
        String name = words[0] + " " + words[1];
        double average = findAverage(words[2], words[3], words[4], words[5], words[6]);
        System.out.println("Name : " + name + " Quiz Avg: " + average);

        //these lines are not needed but OP asked for the values to be stored in arrays
        names.add(name);
        averages.add(average);
    }
  }

  private static double findAverage(String a, String b, String c, String d, String e) {
    double sum = Double.parseDouble(a) + Double.parseDouble(b) + Double.parseDouble(c) + Double.parseDouble(d) + Double.parseDouble(e);
    return (sum / NAMELIMIT);
  }
}

If you do need to store these values for later, I suggest making use of the fact that Java is an Object oriented language and declaring a Student object which can hold the names and grades of your students. You could do that like this:

public class Student {
  private ArrayList<Integer> grades;
  private String fName;
  private String lName;

  public Student(String inputLine) {
    grades = new ArrayList<>();
    String[] lineSplit = inputLine.split(" ");
    fName = lineSplit[0];
    lName = lineSplit[1];
    for (int i = 2; i < lineSplit.length; i++) {
      grades.add(Integer.parseInt(lineSplit[i]));
    }
  }

  private double computeAvg() {
    double sum = 0;
    for (Integer grade : grades) {
      sum = sum + grade;
    }
    return sum / grades.count();
  }

  @Override
  public String toString() {
    return "Name: " + fName + " " + lName + " Quiz Avg: " + computeAvg();
  }
}

private static final int NAMELIMIT = 5;

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

    for (int i = 0; i < NAMELIMIT; i++) {
        String line = in.nextLine();
        Student s = new Student(line);
        System.out.println(s);
    }
}