Mohammed Khasim P
Mohammed Khasim P

Reputation: 35

How do I stop overwriting arraylist?

I have used the input to get values and add it to ArrayList. When I input data for the second time, it overwrites the first data after entering second data. Unable to fix it.

Student class

public class Student {
static int rollNumber, age;
static String firstName, lastName, gender;
public Student(int rollNumber, String firstName, String lastName, int age, String gender) 
 {
   this.rollNumber = rollNumber;
   this.firstName = firstName;
   this.lastName = lastName;
   this.age = age;
   this.gender = gender;
  }
}

InputParticipant class

import java.util.*;
public class InputParticipant extends Student{
        public static String sportsEvent;
        public InputParticipant(int rollNumb, String firstName, String lastName,
        int age, String gender, String sportsEvent) 
        {
            super(rollNumber, firstName, lastName, age, gender);
            this.sportsEvent=sportsEvent;
        }
        public void elegibility()
        {
            System.out.print("Eligibility:");
            //50 meter race
            if(age>=10 && age<=12 && gender.equals("female") && sportsEvent.equals("50meterrace"))
            {
                System.out.println(" Yes ");
            }
            else if((age>=10 && age<=15) && sportsEvent.equals("100meterrace"))//100 meter race
            {
                System.out.println(" yes ");
            }
            else if(age>=15 && (sportsEvent.equals("400meterrace") || sportsEvent.equals("Juvelin") || sportsEvent.equals("discus")))//400 meter race, Juvelin and discus
            {
                System.out.println(" Yes ");
            }
            else
            {
                System.out.println(" No (age:Min 10-12 for 50m race-female only, Min 10-15 for 100m race and Min 15 for 400m race, Juvelin and discus)");
            }
    
         }
         public static void main(String[] args) 
         {
             List<InputParticipant> stud=new ArrayList<InputParticipant>();
             InputParticipant i;
             i=new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent);
             Scanner in=new Scanner(System.in);
             while(true)
             {
                System.out.println("Enter the Student details:");
                System.out.print("roll number:");
                rollNumber=in.nextInt();
                System.out.print("First name:");
                firstName=in.next();
                System.out.print("Last name:");
                lastName=in.next();
                System.out.print("age:");
                age=in.nextInt();
                System.out.print("gender:");
                gender=in.next();
                System.out.print("sports event(50meterrace/100meterrace/400meterrace/Juvelin/discus):");
                sportsEvent=in.next();
                stud.add(i);
                List<InputParticipant> l=new ArrayList<InputParticipant>();
                l.addAll(stud);
                Iterator<InputParticipant> disp= l.iterator();
                while(disp.hasNext())
                {
                    InputParticipant st=disp.next();
                    System.out.println("Roll Number:"+st.rollNumber+"\nName: "+st.firstName+" "+st.lastName+"\nGender: "+st.gender+"\nAge: "+st.age+"\nSports Event:"+st.sportsEvent);
                    st.elegibility();
          System.out.println("-----------------------------------------");
                }
            }
        }
   }

Upvotes: 0

Views: 475

Answers (4)

Usman Shahid
Usman Shahid

Reputation: 101

The members of both the Student class and the InputParticipant class are static. Read up static members. In other words, the ArrayList is not being overwritten, a static member is kind of like a shared variable across the different objects of the same class. If you assign something to a static member in one object, the member will have the new value in all the objects. Just remove the static keyword and make them instance members.

public class Student {
public int rollNumber, age;
public String firstName, lastName, gender;
public Student(int rollNumber, String firstName, String lastName, int age, String gender) 
 {
   this.rollNumber = rollNumber;
   this.firstName = firstName;
   this.lastName = lastName;
   this.age = age;
   this.gender = gender;
  }
}

Upvotes: 0

rdhaese
rdhaese

Reputation: 150

I can't really help you fix the code because a lot is rather strange about it.

Why it seems you are always overriding has probably to do with making all class members of Student static, which means that every instance of the Student class will have the same name, firstname,...

-Maybe start by creating a Student class that only contains private members with getters and setters and a constructor for all those properties.

-Then create your main method in a separate Main class, this will prevent you from doing stuff you shouldn't be doing.

-In that method instantiate a List, AFTER this start a loop.

-In that loop you collect your data of 1 student into local variables (String name = in.readNext(); String firstName =....)

-Now you instantiate a Student object with the constructor, you just pass the local vars into it.

-Add the freshly created Student object to your list.

-When the loop is done, you should have a list filled with Students.

Upvotes: 0

PankajT
PankajT

Reputation: 145

Possibly try using Java POJO.

In your case, AFTER you get the values from Scanner, create a new Object and then add that object to your ArrayList.

i = new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent);
stud.add(i);

Not sure why you are creating another ArrayList inside your while loop, as while should only be used for gathering inputs.

Upvotes: 0

Vugar Suleymanov
Vugar Suleymanov

Reputation: 317

Because you create new object inside loop : List l=new ArrayList();

Try something like this :

public static void main(String[] args) {
    List<InputParticipant> stud=new ArrayList<InputParticipant>();
    //List<InputParticipant> l=new ArrayList<InputParticipant>();

    Scanner in = new Scanner(System.in);
    while(true){
        System.out.println("Enter the Student details:");
        System.out.print("roll number:");
    rollNumber=in.nextInt();
    System.out.print("First name:");
    firstName=in.next();
    System.out.print("Last name:");
    lastName=in.next();
    System.out.print("age:");
    age=in.nextInt();
    System.out.print("gender:");
    gender=in.next();
    System.out.print("sports event(50meterrace/100meterrace/400meterrace/Juvelin/discus):");
    sportsEvent=in.next();
    stud.add(i);
    InputParticipant i = new InputParticipant(rollNumber, firstName, lastName, age, gender, sportsEvent);

    Iterator<InputParticipant> disp = l.iterator();
    while(disp.hasNext()){
        InputParticipant st=disp.next();
        System.out.println("Roll Number:"+st.rollNumber+"\nName: "+st.firstName+" "+st.lastName+"\nGender: "+st.gender+"\nAge: "+st.age+"\nSports Event:"+st.sportsEvent);
        st.elegibility();
        System.out.println("-----------------------------------------");
    }
    }
    } 

Upvotes: 2

Related Questions