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++;
    }
}

      

+3


source to share


3 answers


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 each scan

    , you can just use the same one (also initialize it outside of the loop like a list of arrays).

  • i=i++

    may simply be i++

    , i++

    it increments the variable i, it differs from i + 1

    . This is equivalent i = i + 1

    .

  • It is useful to use an interface List

    on a variable ArrayList

    so that your code works the same regardless of the implementation you use List

    .

+2


source


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;


}

      

+1


source


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++;
    }
}

      

+1


source







All Articles