Multiple objects in an array (java)
I want to create objects using the result from Scanner
and add them to an array.
However, every time I ask for user input a second time, it just overwrites the first object.
How do I add multiple objects to an 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++;
}
}
source to share
Your loop does not overwrite the first value. It just creates new ArrayList
every time you loop.
You have to create one ArrayList and add to it constantly.
ex.
ArrayList<Game> games = new ArrayList<Game>();
while(...) {
...
games.add(...);
...
}
Couple notes:
-
You don't need to create
new Scanner
for eachscan
, you can just use the same one (also initialize it outside of the loop like a list of arrays). -
i=i++
may simply bei++
,i++
it increments the variable i, it differs fromi + 1
. This is equivalenti = i + 1
. -
It is useful to use an interface
List
on a variableArrayList
so that your code works the same regardless of the implementation you useList
.
source to share
Move list initialization outside of the loop. Currently you are creating a list object every time you loop. From here the link to the existing one is lost.
public ArrayList<Game> ajoutadd() {
int i=0;
boolean boucle=true;
ArrayList<Game> thegame = new ArrayList<Game>();
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)
thegame.add(new Game(jname,jqty,categ,jprice));
each time after the loop
i++;
}
System.out.println(thegame);
return thegame;
}
source to share
Create one Scanner
outside the loop and one instance List
(and I suggest you use an interface). How,
public void ajoutadd() {
int i = 0;
boolean boucle = true;
Scanner thegame = new Scanner(System.in);
List<Game> thegame = new ArrayList<>();
while (i != 2) {
System.out.print("name: \n");
String jname = thegame.nextLine();
System.out.print(jname);
System.out.print("qty \n");
int jqty = thegame.nextInt();
System.out.print("cat: \n");
String categ = thegame.nextLine();
System.out.print("price: \n");
int jprice = thegame.nextInt();
Game agame = new Game(jname, jqty, categ, jprice);
System.out.print(unjeu.Nom);
thegame.add(new Game(jname, jqty, categ, jprice));
System.out.println(thegame);
i++; // <-- not i = i++;
}
}
source to share