-2

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.

3 Answers3

1

What you want to do is change

 Student student1 = new Student();

to take in the scanner instance like this :

 Scanner sc = new Scanner (System.in)
 Student student1 = new Student(sc);
 Student student2 = new Student(sc);
 .
 .
 .
 Student student7 = new Student(sc);

This should solve most of your problems. This error you are getting is because you don't have an empty constructor defined for the student class. You are override the default constructor with a constructor that requires arguments.

You could change you student class to :

public class Student {

    private String studentMajor;
    private String studentName;

    public Student(String major, String name) {
        this.studentMajor = major;
        this.studentName = name;
    }

    public Student(Scanner kb) {
        System.out.println("Please insert the student's name.");
        this.studentName = kb.nextLine();
        System.out.println("Please insert the student's major.");
        this.studentMajor = kb.nextLine();
    }

    // Add getter for your fields here.

    @Override
    public String toString(){
        return studentName +": "+ studentMajor+"\n";
    }

}

Next Question you can iterate over the list like this :

for(Student eachStudent : student) {
     System.out.println(eachStudent);
}

Last question you could do

int totalNumOfStudents = 10;
Scanner sc = new Scanner(System.in);
for(int counter = 0; counter < totalNumOfStudents; counter++) {
    student.add(new Student(in));
}

or you can do

student.add(new Student("major","name"));

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • Thank you so much, that is one problem out of the way. Now the next question is will this actually add whatever the user types in to the list? Is my code correct to do that? – MoeMarmalade Nov 24 '15 at 15:45
  • 1
    You could mention that you should not create more than one `Scanner` instance for `System.in`: http://stackoverflow.com/questions/13950154/java-util-scanner-wait-for-input (you should probably not do it in the Student constructor) – Andreas Fester Nov 24 '15 at 15:45
  • @AndreasFester thanks completely forgot to mention that, – StackFlowed Nov 24 '15 at 15:46
  • @MoeMarmalade The constructor itself doesn't add ``this`` to the list – Binkan Salaryman Nov 24 '15 at 15:46
  • @StackFlowed Thank you so much, but I literally completely forgot to say that in this assignment I have been told specifically NOT to use setters and getters for my methods in the Student class. That is the part that is throwing me off the most. However after testing my code, it asked for 7 different names and majors, so it must be working I am guessing. – MoeMarmalade Nov 24 '15 at 15:52
  • @StackFlowed Actually, with the code you have provided me, I have managed to figure out everything I needed to know. Turns out I was over complicating things. Thank you so much for all your help! – MoeMarmalade Nov 24 '15 at 16:02
0

1) There is no parameterless constructor defined in your class Student. Provide a default constructor (e.g. with a new Scanner(System.in)) as scanner) or input the parameters.

2) Remove the "\n" part from the toString method as it isn't related, then print out the list (using an iterator) as follows:

for(Student s : student) {
    System.out.println(s);
}

3) Have you ever heard of for loops? They're handy:

Scanner in = new Scanner(System.in);
for(int i = 0; i < 7; i++) {
    student.add(new Student(in)); // or any other constructor call
}
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
0

To fully understand the concepts, you can get more information & gain further practice here:
For 1: Java Class constructors
For 2 & 3: Java For loops

Leet-Falcon
  • 2,107
  • 2
  • 15
  • 23