My Main Class
package edu.bsu.cs121.mamurphy;
import java.io.*;
import java.util.*;
public class Lab6Main {
static LinkedList<Student> student = new LinkedList<Student>();
public static void main(String[] args){
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
Student student4 = new Student();
Student student5 = new Student();
Student student6 = new Student();
Student student7 = new Student();
student.add(student1);
student.add(student2);
student.add(student3);
student.add(student4);
student.add(student5);
student.add(student6);
student.add(student7);
}
}
My Student Class
package edu.bsu.cs121.mamurphy;
import java.util.Scanner;
public class Student {
public String studentMajor;
public String studentName;
public Student(String major, String name) {
studentMajor = major;
studentName = name;
}
public Student(Scanner kb) {
System.out.println("Please insert the student's name.");
studentName = kb.nextLine();
System.out.println("Please insert the student's major.");
studentMajor = kb.nextLine();
}
@Override
public String toString(){
return studentName +": "+ studentMajor+"\n";
}
}
I am currently being assigned to create a linked list that will hold both a Student's Name and Student's Major as a student object.
I have to make 7 student objects that are being put into the list and I have to use a toString method in my Student class to make the output of the iterator name: major
.
I am currently stuck on a few things:
1) My Student student1 - 7 = new Student();
is giving me errors. It wants me to either change the argument to match the scanner or to match the (String, String)
that I have for the first Student constructor. Doing either of those things just gives me more errors.
2) How do I implement the iterator to actually do what it is supposed to do? I have to use the iterator to go through the linked list of students and print out each and every one of the objects in it with a specific format in mind: name: major
. I believe I have the formatting correct with my toString method, but I am not sure.
3) Is there any way to create the 7 student objects without having to write out each and every one? I am asking this for the sake of cleaning up my code.
I know I am asking a lot, but any help is greatly appreciated.