Re: noob question about boolean methods
In article
<239e9bdc-d434-4028-a8aa-f9b5c3589417@e1g2000pra.googlegroups.com>,
justineee <aguas.justine@gmail.com> wrote:
Hello, I am checking if the ArrayList contains the string sales and
cost of goods sold.. I know the ArrayList contains the words but when
I check it through a method.. it returns false.
public boolean contains()
{
boolean x=false;
if (list.contains("sales")==true
&& list.contains("cost of goods sold")==true)
{
x=true;
return x;
}
return x;
}
this is the method.. when I call this in System.out.print(), It shows
false but at the same time.. I displayed the words inside the
ArrayList and sales and cost of goods sold were displayed.. I think
the error is in this method.. Any help?
Why not just this?
public boolean contains() {
return list.contains("sales")
&& list.contains("cost of goods sold");
}
We'll need to see your list, too. In the interim, you might look at the
API for List#contains():
<code>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ContainsTest {
private static final String S1 = "sales";
private static final String S2 = "cost of goods sold";
private static final String S3 = "not appearing in this film";
private static final List<String> list =
new ArrayList<String>(Arrays.asList(S1, S2));
public static void main(String[] args) {
System.out.println(list.contains(S1) && list.contains(S2));
System.out.println(list.contains(S3));
}
}
</code>
<console>
true
false
</console>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>