简体   繁体   中英

adding elements to LinkedList using for loop and user input

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

]

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 Student s". 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM