2

I tried to do a students registration program. So I wrote a class file also. Using that class file I put values to an ArrayList. But the problem is, If I enter details about 3 students, at last It only prints only the details about the last student twice. I'm a begginer and please help me to overcome this problem.

Here is the main method I wrote;

    Student obj = new Student();

    Scanner in = new Scanner(System.in);

    ArrayList<Student> List = new ArrayList<Student>();

    for (;;) {
        System.out.print("Enter the Student ID: ");
        obj.setStudentID(in.next());

        System.out.print("Enter the first name: ");
        obj.setFirstName(in.next());

        System.out.print("Enter the last name: ");
        obj.setLastName(in.next());

        System.out.print("Enter the Course Work 1 marks: ");
        obj.setCwk01(in.nextInt());

        System.out.print("Enter the Course Work 2 marks: ");
        obj.setCwk02(in.nextInt());

        System.out.print("Enter the final exam marks: ");
        obj.setExam(in.nextInt());

        List.add(obj);

        System.out.print("Enter q to stop (or any other to continue): ");
        String str = in.next();
        if (str.equals("q") || str.equals("Q")) {
            System.out.println("\n");
            break;
        }

        System.out.println("\n");
    }
    for (Student value : List) {
        System.out.println(value.getCwk01());
    }

and here's the class file;

public class Student {

    private String studentID;
    private String firstName;
    private String lastName;
    private int cwk01;
    private int cwk02;
    private int exam;

    private int fMarks;

    public int getfMarks() {
        return fMarks;
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getCwk01() {
        return cwk01;
    }

    public void setCwk01(int cwk01) {
        this.cwk01 = cwk01;
    }

    public int getCwk02() {
        return cwk02;
    }

    public void setCwk02(int cwk02) {
        this.cwk02 = cwk02;
    }

    public int getExam() {
        return exam;
    }

    public void setExam(int exam) {
        this.exam = exam;
    }

    public void calculateFMarks() {
        this.fMarks = (int) ((this.cwk01 * 0.2) + (this.cwk02 * 0.2) + (this.exam * 0.6));
    }
}
Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
user9480
  • 192
  • 12

2 Answers2

5

Move this declaration:

Student obj = new Student();

inside the for loop. Currently you're mutating the same object and adding it's reference inside the list. At the end, all the references will be pointing to same object, and hence will print out the same details when you print them.

Apart from that, there are few more issues that I see in your code:

Please change the variable name List to list, or even better studentList.

The below if condition:

if (str.equals("q") || str.equals("Q"))

can be replaced with:

if (str.equalsIgnoreCase("q"))

Also, whenever you are reading some value using Scanner#nextInt() or Scanner#next() or any other method, make sure there is inputs available to read, else it might throw exception at runtime. You can use Scanner#hasNextInt() or Scanner#hasNext() respectively.

And finally, there is a minor bug in your code at this line:

String str = in.next();

Note that, you are invoking in.next() after in.nextInt(). That will cause you issue, as explained in this post. Follow the workaround in that post, and you'll be fine.

Oh, and yes, System.out.println("\n"); can be replaced with System.out.println();, unless you really want to print 2 newlines.

Community
  • 1
  • 1
Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
0

You need to create a new Student object each time the for loop runs.

for (;;) {
    final Student obj = new Student();
    // ...
}

You could remove the set methods from the Student class and only allow fields to be set in the Student constructor, to avoid the possibility of this error.

Stuart Caie
  • 2,563
  • 12
  • 14