Michael Grinnell Michael Grinnell - 1 month ago 9
Java Question

How can I store a string and two parallel arraylists in an object. (java)

I want to store a student name and for each name have two ArrayLists associated with that name. I would like to have it all in one class Student_quizes or something along those lines

so im basicly trying to store studentName, quizNames arraylist and quizScores arraylist in an object. quizNames and quizScores will be parallel.

I am not sure if im going about this the right way at all Ive been trying this all day and am quite confused, Does anyone have any suggestions on how I could better do this.

Im sorry if my question is confusing, I imagine the program would run like this.


input student name

michael

input quiz

science

input quiz score

80

add another quiz Y/N?

Y

input quiz ...

add another student Y/N?

Y
...

Student name : michael

quiz name : science, Score: 80

quiz name : maths, Score : 85

student name : joe

Quiz name : science, Score 60


My code is all over the place at the moment but ill post what I have.

This is the main




package Student_Quizes;

import java.util.ArrayList;
import java.util.Scanner;
import static student.student_main.print;

public class Student_quizes_main {

public static void main(String[] args) {

boolean addStudents = true;
addStudent();

}

public static void addStudent() {

ArrayList<Student_Quizes> students = new ArrayList<>();

Scanner in = new Scanner(System.in);
System.out.println("add student");
String studentName;
String Continue;
int quizScore;
String quizName;

boolean addStudents = true;
boolean addQuiz = true;

while (addStudents) {

System.out.println("Student Name");
studentName = in.next();

while (addQuiz) {

System.out.println("Quiz Name");
quizName = in.next();
System.out.println("average Score");
quizScore = in.nextInt();
students.add(new Student_Quizes(quizName, quizScore));

System.out.println("Add another Quiz Y/N");
Continue = in.next();

if (Continue.equalsIgnoreCase("n")) {
addQuiz = false;
}

}

System.out.println("Add another Student Y/N");
Continue = in.next();

if (!Continue.equalsIgnoreCase("n")) {

addQuiz = true;
} else {
addStudents = false;
}

}

print(students);

}

}


this is my classes




package Student_Quizes;

import java.util.ArrayList;

public class Student_Quizes {

private String studentName;
private String quizName;
private int score;
ArrayList<String> quizNames = new ArrayList<>();
ArrayList<Integer> quizScores = new ArrayList<>();

public Student_Quizes() {
studentName = "";
quizNames.add("");
quizScores.add(0);
}

public Student_Quizes(String studentName, ArrayList QuizNames, ArrayList quizScores) {
this.studentName = studentName;
this.quizNames.add(quizName);
this.quizScores.add(score);

}

public void print() {
System.out.println("Name:\t\t" + this.studentName);
System.out.println("Quiz Name:\t" + this.quizNames);
System.out.println("Quiz Score:\t" + this.quizScores);

}
}

Answer

Some quick hint:

public class Student_Quizes {
  private String studentName;
  private String quizName; ...

is actually a good start, but you get it wrong here:

this.quizNames.add(quizName);
this.quizScores.add(score);

You see: you want that your class Student_Quizes allows to add the two pieces of information. Thus: you do not put anything into your two lists in the constructor. Instead, you add a method like

public void addQuizInfo(String name, Integer score) {
  quizNames.add(name);
  quizScores.add(score);
}

to your class. And then, you just keep calling this method with the input from the user!

Your problem is basically that you pulled things together "repeatedly". Instead, you should clearly separate things. To make that more clear; already your class names goes in the wrong direction. Call it Student ... and then think what properties a student has --- he has a name, and then he has lists with the quizes he worked on.

So, you first create a student object:

Student newStudent = new Student(name);

and then you use that method I showed above:

student.addQuiz(quizname, score);

And finally: your code wouldn't even compile. You declared your lists within Student_Quizes to be

ArrayList<String>

for example. Such a lists only takes string. Thus the code you have their to add ... another list is simply invalid!

Finally: you should use the "interface" types such as List, Map, and so on as type - you only use the concrete implementation class such as ArrayList for the call to new, like

List<Integer> scores = new ArrayList<>();