search method doesn't seem to work

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 839 Views - Last Post: 24 August 2010 - 08:15 AM Rate Topic: -----

Topic Sponsor:

#1 webhound2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 06-July 10

search method doesn't seem to work

Posted 22 August 2010 - 11:22 AM

I'm trying to search this ArrayList but it comes back false every time. Below are the two methods that interact with one another. And below that is the TallyItem class that comprises the ArrayList.

public int getItemCount(String item)
	{


		if(!items.isEmpty())
		{
			this.item=item;

			if(searchArray(item))
			{
				count=tempItem.getCount();
				return count;
			}

			else
			{
				JOptionPane.showMessageDialog(null, "The item, " + item +", doesn't exist.", "Item Not Found", JOptionPane.ERROR_MESSAGE);
				return 0;
			}
		}
		else
		{
			JOptionPane.showMessageDialog(null, "There are no items in the tally.", "Item Not Found.", JOptionPane.ERROR_MESSAGE);
			return 0;
		}

	}



public boolean searchArray(String item)
	{
		counter=0;
		while(!found && counter<items.size())
		{
			tempItem = items.get(counter);
			if(tempItem.getName()==item)
			{
				found = true;
				return found;
			}
			else
				counter++;

		}
		return found;
	}



public class TallyItem
{
	private static String name;
	private static int count;

	public TallyItem(String name, int count)
	{
		this.name=name;
		this.count=count;
	}

	public static String getName()
	{
		return name;
	}

	public static int getCount()
	{
		return count;
	}

	public static void setCount(int addCount)
	{
		count+=addCount;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: search method doesn't seem to work

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: search method doesn't seem to work

Posted 22 August 2010 - 11:31 AM

Strings are compared for equality with the equals() method, the "==" operator will only test to see if it is the same object w/ same reference.

I think you are overcomplicating your search. If you want to search to see if an item is in your arraylist, just iteratre through until you stumble across it.
public int searchArray(String name)
{
  for(int i = 0; i < yourArray.size(); i++)
  {
    if(yourArray.get(i).getName().equals(name))
      return i;
  }
  return -1;
}


Was This Post Helpful? 1
  • +
  • -

#3 webhound2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 06-July 10

Re: search method doesn't seem to work

Posted 22 August 2010 - 11:57 AM

Thanks, for that bcranger. I'm still learning this stuff and that's real helpful. I always wonder about concatenating*? method call. This helps alot. I could've sworn I'd compared strings to empty strings using "" and == before, but this seems like a better way to do it.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7494
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: search method doesn't seem to work

Posted 22 August 2010 - 12:00 PM

Behind the scenes, there is a String pool to help with memory efficiency. As an example, rather than have 1000 "" String objects in memory, only 1 "" is stored in memory, and it is continually referenced. So b/c the memory addresses are the same, the == operator works. However, you are guaranteed an accurate check with equals() vs. ==. The same applies to all Strings with the String pool, just using "" as an example.
Was This Post Helpful? 0
  • +
  • -

#5 webhound2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 06-July 10

Re: search method doesn't seem to work

Posted 22 August 2010 - 02:02 PM

View Postmacosxnerd101, on 22 August 2010 - 11:00 AM, said:

Behind the scenes, there is a String pool to help with memory efficiency. As an example, rather than have 1000 "" String objects in memory, only 1 "" is stored in memory, and it is continually referenced. So b/c the memory addresses are the same, the == operator works. However, you are guaranteed an accurate check with equals() vs. ==. The same applies to all Strings with the String pool, just using "" as an example.


I'm pretty new at this. What is a String pool? Does it have anything to do with args in the main method?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7494
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: search method doesn't seem to work

Posted 22 August 2010 - 02:11 PM

The String pool allows for storing unique Strings rather than a String each time one is assigned. So:
String x = "";
String y = "";



There is only one "" in memory b/c of the String pool. There are plenty of links if you want to read more about it, but the String pool makes it safer in certain instances to compare Strings using ==, but to be 100% sure, compare using equals().
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: search method doesn't seem to work

Posted 22 August 2010 - 06:52 PM

View Postmacosxnerd101, on 22 August 2010 - 04:11 PM, said:

The String pool allows for storing unique Strings rather than a String each time one is assigned. So:
String x = "";
String y = "";



There is only one "" in memory b/c of the String pool. There are plenty of links if you want to read more about it, but the String pool makes it safer in certain instances to compare Strings using ==, but to be 100% sure, compare using equals().


That's pretty advanced for him I think. However, if you think about every object that you make as simply a gateway to the memory, then this makes sense. In mac's example, those aren't two separate objects, they are two different gateways (references) to the SAME piece of memory because the values of the Strings are the same. Make sense?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7494
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: search method doesn't seem to work

Posted 22 August 2010 - 06:53 PM

Maybe a little advanced and not really necessary to know at this point if he uses equals() exclusively, but just providing more explanation in regards to his question about why == works sometimes with Strings. :)
Was This Post Helpful? 0
  • +
  • -

#9 webhound2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 06-July 10

Re: search method doesn't seem to work

Posted 22 August 2010 - 07:52 PM

View Postmacosxnerd101, on 22 August 2010 - 05:53 PM, said:

Maybe a little advanced and not really necessary to know at this point if he uses equals() exclusively, but just providing more explanation in regards to his question about why == works sometimes with Strings. :)



It makes sense, I think. When using == you're looking for a memory location instead of the item within the quotes...I think. So you end up asking whether or not the item is in the same location instead of asking for exact equality. That's what I make of it, anyway.
Was This Post Helpful? 1
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: search method doesn't seem to work

Posted 23 August 2010 - 03:40 AM

View Postwebhound2, on 22 August 2010 - 09:52 PM, said:

It makes sense, I think. When using == you're looking for a memory location instead of the item within the quotes...I think. So you end up asking whether or not the item is in the same location instead of asking for exact equality. That's what I make of it, anyway.


Yep. You got it. Most of the time, that's a bad thing to do with objects( == ), but with certain Strings, its ok to do.
Was This Post Helpful? 0
  • +
  • -

#11 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 492
  • View blog
  • Posts: 1,448
  • Joined: 19-October 09

Re: search method doesn't seem to work

Posted 23 August 2010 - 01:58 PM

@webhound2, If you want to check whether a specific element is in the ArrayList, you can use the contains() method thats returns true/false depending on if the element in the params being tested is in the ArrayList or not.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,892
  • Joined: 06-March 08

Re: search method doesn't seem to work

Posted 23 August 2010 - 04:40 PM

View Postm-e-g-a-z, on 23 August 2010 - 02:58 PM, said:

@webhound2, If you want to check whether a specific element is in the ArrayList, you can use the contains() method thats returns true/false depending on if the element in the params being tested is in the ArrayList or not.

Actually never checked that one :)
Returns true/false depending on if the element in the params being tested is in the ArrayList or not
or
Returns true if the element in the params being tested equals method return true when compared with one of the element in the arraylist

???
Was This Post Helpful? 1
  • +
  • -

#13 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: search method doesn't seem to work

Posted 23 August 2010 - 04:47 PM

View Postpbl, on 23 August 2010 - 03:40 PM, said:

View Postm-e-g-a-z, on 23 August 2010 - 02:58 PM, said:

@webhound2, If you want to check whether a specific element is in the ArrayList, you can use the contains() method thats returns true/false depending on if the element in the params being tested is in the ArrayList or not.

Actually never checked that one :)
Returns true/false depending on if the element in the params being tested is in the ArrayList or not
or
Returns true if the element in the params being tested equals method return true when compared with one of the element in the arraylist

???

Oops...pressed +1 instead of reply lol

But the answer to your question is the former. Just checks to see if param is contained within array.
Was This Post Helpful? 0
  • +
  • -

#14 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 492
  • View blog
  • Posts: 1,448
  • Joined: 19-October 09

Re: search method doesn't seem to work

Posted 23 August 2010 - 05:02 PM

View Postbcranger, on 23 August 2010 - 10:47 PM, said:

Oops...pressed +1 instead of reply lol

But the answer to your question is the former. Just checks to see if param is contained within array arraylist.


Make things clearer :)

This post has been edited by m-e-g-a-z: 23 August 2010 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,892
  • Joined: 06-March 08

Re: search method doesn't seem to work

Posted 23 August 2010 - 06:44 PM

You are wrong bot of you :)
Here is the proof:

public class Contains {
	public static void main(String[] args) {
		String a, c;
		
		// just to make sure the 2 String do not point to the same object
		a = new String("abc");
		c = new String("abc");
		System.out.println("Is a == c: " + (a == c) + " just to be sure");
		
		ArrayList<String> al = new ArrayList<String>();
		al.add(a);
		// test if contained
		System.out.println("Does the ArrayList contains c: " + al.contains(c));
	}
}


prints out
Is a == c: false just to be sure
Does the ArrayList contains b: true



So the contains() method check if the searched object equals method returns equality with an object contained in the ArrayList not if the searched object is effectively in the ArrayList

So the code of contains should look like

boolean contains(Object o) {
   for(int i = 0; i < size; i++) {
     if(array[i].equals(o))
       return true;
   }
   return false;
}


and not
boolean contains(Object o) {
   for(int i = 0; i < size; i++) {
     if(array[i] == o)
       return true;
   }
   return false;
}


as you seem to think (to though I hope)

After all bcranger my +1 was desserved :)

This post has been edited by pbl: 23 August 2010 - 06:47 PM
Reason for edit:: Dammed b) changed to c

Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2