0

I am trying to use a for loop to add elements to a LinkedList, and whenever I print the list it only contains one of my inputs, and has put it at every index.

I have a feeling the problem lies with my toString method, or with my Student constructor but can't seem to figure it out.

Any and all help is appreciated. Thanks!

 import java.util.*;

public class Student {

    private static String name;
    private static String address;
    private static double GPA;
    static LinkedList<Student> stu = new LinkedList<Student>();
    static Scanner scanner = new Scanner(System.in);


    public Student(String name, String address, double GPA) {
        Student.name = name;
        Student.address = address;
        Student.GPA = GPA;
    }

    public String getName() {
        return Student.name;
    }

    public String getAddr() {
        return Student.address;
    }

    public double getGPA() {
        return Student.GPA;
    }

    public static void main(String [] args) {
        for (int i = 0; i <= 2; i++) {
            System.out.println("Enter the student's name: ");
            name = scanner.next();
            System.out.println("Enter the student's address: ");
            address = scanner.next();
            System.out.println("Enter the student's GPA: ");
            GPA = scanner.nextDouble();
            stu.addLast(new Student(name, address, GPA));
        }
        System.out.println(stu);
    }

    @Override
    public String toString() {
    String str = "Name: " + getName() + "\nAddress: " + getAddr() + "\nGPA: " + getGPA()+ "\n\n";
    return str;
    }
}

Console

Enter the student's name: 
Jim
Enter the student's address: 
111Ave
Enter the student's GPA: 
2.3
Enter the student's name: 
Joe
Enter the student's address: 
222Ave
Enter the student's GPA: 
3.0
Enter the student's name: 
Jack
Enter the student's address: 
333Ave
Enter the student's GPA: 
3.4
[Name: Jack
Address: 333Ave
GPA: 3.4

, Name: Jack
Address: 333Ave
GPA: 3.4

, Name: Jack
Address: 333Ave
GPA: 3.4

]
shepherx
  • 15
  • 3
  • You never call `nextLine()` on your scanner, so you're just reading the same input line over and over again. (see https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – azurefrog Jan 06 '20 at 23:14
  • @azurefrog After modifying the program with nextLine() it is still only printing the last Student object I enter at every index. Any tips? – shepherx Jan 06 '20 at 23:22
  • Ah yes, you've made name, address and GPA static, so there's only one value for the class, rather than each object having its own value. – azurefrog Jan 06 '20 at 23:25
  • @azurefrog Yes, I have been trying to avoid doing that lately (bad habit of mine). How should I modify it? Should I move the for loop out of main? – shepherx Jan 06 '20 at 23:29

1 Answers1

2

The attributes name, address, and GPA are static, which means they're accessed from all the student objects you create. Thus when you create a new student object and call it's constructor you change the values of name, address, and GPA for all the other student objects you've created before.

To solve your problem you need to remove the static keyword from the declaration of name, address, and GPA.

Now all that's left is to change the way you access your variables. Notice how you used to use Student.name whenever you wanted to use the attribute name? this only works when name is static "aka name is the same for all Students". We now want to use the name of the current student not All students, so we should use this.name instead of Student.name. Similarly change Student.GPA to this.GPA and Student.address to this.address.

Also you can't just use the attributes name, address, and GPA inside your main without an object "since they're not static anymore", so you'll need to declare name, address, and GPA inside your main, notice that these variables are not related to the variable attributes inside the Student class. Please refer to this code for better understanding, sorry for my terrible explanation.

import java.util.*;

public class Student {

    private String name;
    private String address;
    private double GPA;
    static LinkedList<Student> stu = new LinkedList<Student>();
    static Scanner scanner = new Scanner(System.in);


    public Student(String name, String address, double GPA) {
        this.name = name;           //this.name instead of Student.name
        this.address = address;     //this.address instead of Student.address
        this.GPA = GPA;             //this.GPA instead of Student.GPA
    }

    public String getName() {
        return this.name;           //similarly
    }

    public String getAddr() {
        return this.address;        //similarly
    }

    public double getGPA() {
        return this.GPA;            //similarly
    }

    public static void main(String [] args) {
        for (int i = 0; i <= 2; i++) {
            System.out.println("Enter the student's name: ");

            //notice here. "name" can be changed to anything, "sname" for example
            //this variable is just to store the input, it's not related to the name
            //attribute in the class
            String name = scanner.next();
            System.out.println("Enter the student's address: ");

            //same goes for address and GPA
            String address = scanner.next();
            System.out.println("Enter the student's GPA: ");
            double GPA = scanner.nextDouble();

            //use the input taken above to create a new student by calling the constructor
            stu.addLast(new Student(name, address, GPA));
        }
        System.out.println(stu);
    }

    @Override
    public String toString() {
        String str = "Name: " + getName() + "\nAddress: " + getAddr() + "\nGPA: " + getGPA()+ "\n\n";
        return str;
    }
}
  • How would I restructure my program after taking this advice? Should I put the main method in another class? – shepherx Jan 06 '20 at 23:36
  • Well that's not essential for your code to run. It should run normally with the main inside the class. There are however some other changes you need to make after changing `name`, `address`, and `GPA` into non-static variables. I'll update my answer and explain these changes. – Moustafa El-Sayed Jan 06 '20 at 23:42