3

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

3 Answers 3

2

Your loop is not actually overwriting the first value. It is just creating a new ArrayList every time you loop through.

You should create one ArrayList and continously add to it.

ex.

ArrayList<Game> games = new ArrayList<Game>();
while(...) {
   ...
   games.add(...);
   ...
}

Couple notes:

  • You do not have to create a new Scanner for every scan you can just use the same one (Also initialize it outside the loop like the array list).

  • i=i++ could just be i++, i++ increments the variable i, it is different than i + 1. It is equivalent to i = i + 1.

  • It is good practice to use the List interface for the ArrayList variable so that your code will work the same regardless of which List implementation you use.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot.Good advice and answer.I created only one scanner like you said,however theres something wrong at the cat and price input,when the compiler reach these,it prints cat: and price: and its like if the price: was going over cat: without letting time to input the cat String? I dont know if you understand what i mean.Like I cant input cat,it immediatly jump to price?
1

Create one Scanner outside the loop and one List instance (and I suggest you use the interface). Like,

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

2 Comments

And how do I return the arraylist to be able to use it in other class?
Change public void ajoutadd() { to public List<Game> ajoutadd() { and add a return thegame; after your while loop. Also, you should probably ask a new question (and do some research) on your own.
1

Move initialization of list outside the loop. Currently, you are creating list object everytime in loop. Hence losing the reference to existing one.

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;


}

6 Comments

And how do I return the arraylist to be able to use it in other class?
changed the return type to Arraylist and returned the list. Now you get this value in calling method. Kindly accept the answer or upvote if it works for you.
I did what you told me too,however when I try to call it in other methods, like public void disp(){ System.out.print(thegame);} the game doesnt get recognized...?
While calling from other method save the value in other variable and print them. ArrayList<Game> game = ajoutadd(); System.out.println(game);. thegame has method level scope so will not be recognized outside method but you can get returned value in variables
This is because this variable in scope of method , but we added return statement so that we get value in calling method. disp(){ ArrayList<Game> game = ajoutadd() ; System.out.println(game); } .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.