3 Replies - 255 Views - Last Post: 30 January 2013 - 07:34 PM Rate Topic: ***-- 2 Votes

#1 Staplers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-October 12

ArrayList question

Posted 30 January 2013 - 04:19 PM

I have a question concerning array lists. I have a list of arrays and I already have stored a product name called: item, and a 10 digit code for each product. Example: Coffee 0123456789

Now I am attempting to create a method that takes in the code and returns the item. Here is what I have so far.

public String getItem(String userUPC) {
		for(int i = 0; i < itemList.size(); i++) {
			if(userUPC == itemList.get(itemList(i).UPC) {
				return itemList.get(itemList(i).item);
			}
		}
	}


I know this is wrong but I have just recently been introduced to array lists and I am having trouble figuring this out. Just let me know if this is the right idea and what exactly the method is to get the array list "itemList" UPC code and match is to the one entered in the method in order to return the items name.

Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList question

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • Posts: 2,652
  • Joined: 29-July 11

Re: ArrayList question

Posted 30 January 2013 - 04:56 PM

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

public Item getItem(String upc) {
    for (Item i : items) {
        if (i.getUPC().equals("12345")) {
        return i;
        }
    }
}



Will that do?

Or you can
public String getItem(String upc) {
    for (Item i : items) {
        if (i.getUPC().equals("12345")) {
        return i.toString();
        }
    }
}


This post has been edited by farrell2k: 30 January 2013 - 04:58 PM

Was This Post Helpful? 2
  • +
  • -

#3 Staplers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-October 12

Re: ArrayList question

Posted 30 January 2013 - 05:14 PM

I'm getting an error, but thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • Posts: 2,652
  • Joined: 29-July 11

Re: ArrayList question

Posted 30 January 2013 - 07:34 PM

Well, what's happening?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1