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;
}
}
22 Replies - 839 Views - Last Post: 24 August 2010 - 08:15 AM
#1
search method doesn't seem to work
Posted 22 August 2010 - 11:22 AM
Replies To: search method doesn't seem to work
#2
Re: search method doesn't seem to work
Posted 22 August 2010 - 11:31 AM
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;
}
#3
Re: search method doesn't seem to work
Posted 22 August 2010 - 11:57 AM
#4
Re: search method doesn't seem to work
Posted 22 August 2010 - 12:00 PM
#5
Re: search method doesn't seem to work
Posted 22 August 2010 - 02:02 PM
macosxnerd101, on 22 August 2010 - 11:00 AM, said:
I'm pretty new at this. What is a String pool? Does it have anything to do with args in the main method?
#6
Re: search method doesn't seem to work
Posted 22 August 2010 - 02:11 PM
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().
#7
Re: search method doesn't seem to work
Posted 22 August 2010 - 06:52 PM
macosxnerd101, on 22 August 2010 - 04:11 PM, said:
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?
#8
Re: search method doesn't seem to work
Posted 22 August 2010 - 06:53 PM
#9
Re: search method doesn't seem to work
Posted 22 August 2010 - 07:52 PM
macosxnerd101, on 22 August 2010 - 05:53 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.
#10
Re: search method doesn't seem to work
Posted 23 August 2010 - 03:40 AM
webhound2, on 22 August 2010 - 09:52 PM, said:
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.
#12
Re: search method doesn't seem to work
Posted 23 August 2010 - 04:40 PM
m-e-g-a-z, on 23 August 2010 - 02:58 PM, said:
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
???
#13
Re: search method doesn't seem to work
Posted 23 August 2010 - 04:47 PM
pbl, on 23 August 2010 - 03:40 PM, said:
m-e-g-a-z, on 23 August 2010 - 02:58 PM, said:
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.
#14
Re: search method doesn't seem to work
Posted 23 August 2010 - 05:02 PM
#15
Re: search method doesn't seem to work
Posted 23 August 2010 - 06:44 PM
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
|
|

New Topic/Question
Reply




MultiQuote






|