I want to create objects using the result from Scanner and add them to an array.
However, each time I ask for user input for the second time, it just overwrites the first object.
How can I add multiple objects to the array?
Here's my code:
public void ajoutadd() {
int i=0;
boolean boucle=true;
while(i!=2){
Scanner thegame = new Scanner(System.in);
System.out.print("name: \n");
String jname = lejeu.nextLine();
System.out.print(jname);
Scanner qty = new Scanner(System.in);
System.out.print("qty \n");
int jqty = qty.nextInt();
Scanner cat = new Scanner(System.in);
System.out.print("cat: \n");
String categ = cat.nextLine();
Scanner price = new Scanner(System.in);
System.out.print("price: \n");
int jprice = price.nextInt();
Game agame = new Game(jname,jqty,categ,jprice);
System.out.print(unjeu.Nom);
// creating the array to contain the game(s)
ArrayList<Game> thegame = new ArrayList<Game>();
thegame.add(new Game(jname,jqty,categ,jprice));
// actually only display 1 object that is overwritten
// each time after the loop
System.out.println(thegame);
i=i++;
}
}