0

I have an ArrayList with lots of objects. I want to be able to add any objects into a choice of 3 different LinkedLists. A user input will select which item to add to which LinkedList by typing the index they wish to be added. This is the kind of thing i'm after, but i just can't get it to work:

public void addToRepository(int indx, int r) {
    if (r == 1){ //checks to see if chosen repository 1
        for (int i=0; i < itemList.size(); i++) {
            i = indx;
        } // ignore this it was just me playing around to see if i'd be able to get the index this way..
        repo1.add(itemList.get(indx)); //adds item from "itemList" with the index that
                                         the user input has given
    //other if statements   
    }                                   
}

I'm not sure this is the correct idea but it gives error "Item cannot be converted to String". If not, how can i go about doing this?

5
  • Your for loop looks useless. Why is it there? Also what are the types of variables you are storing in itemList and repo1? Commented Mar 26, 2015 at 4:45
  • Please show us how you define repo1 and itemList. Also, your for loop looks totally bogus. Commented Mar 26, 2015 at 4:47
  • @AniketThakur i was just playing with it, i thought i'd be able to look and with the loop get the index to then add to the linked list. itemList stores Items ArrayList<Item> itemList = new ArrayList<Item>();, here's a print of itemList Item{itemNo=12401, description=15th century table, price=1200.0} Commented Mar 26, 2015 at 4:49
  • I'm with them on the for loop. It looks like repo1 contains strings, based on the compiler complaint. If that's the case, then Item needs to override (implement?) toString() to allow it to be converted. Commented Mar 26, 2015 at 4:49
  • Either that or repo1 needs to contain Item. Commented Mar 26, 2015 at 4:51

1 Answer 1

1

So you have

ArrayList<Item> itemList = new ArrayList<Item>();

and you are trying to do -

repo1.add(itemList.get(indx)); 

As per the Exception you are getting it looks like repo1 has String data. You can do one of the following things -

  1. Use repo1.add(itemList.get(indx).toString()); OR
  2. Change repo1 generics to include Item data instead of String
Sign up to request clarification or add additional context in comments.

9 Comments

Note that he has to implement or override Item.toString() in order for the first solution to work.
Yup don't miss overriding toString() method or else the implementation from Object will be used and you will get something like class name @ hashcode which may not make any sense to the user.
@RonThompson Item already has a toString. So what's the difference if i change the LinkedLists to include item instead?
If item already has toString() that solution would be faster, but changing repo to contain Items is probably the smarter move.
Just declare it as LinkedList<Item> repo1 = new LinkedList<>();
|

Your Answer

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