noob question about boolean methods

J

justineee

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?
 
M

Mark Space

justineee said:
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.


Works for me.

run:
true
BUILD SUCCESSFUL (total time: 1 second)


package fubar;

import java.util.ArrayList;


public class ListTest {
static ArrayList list = new ArrayList();

public static boolean contains()
{
boolean x=false;

if (list.contains("sales")==true && list.contains(
"cost of goods sold")==true)
{
x=true;
return x;
}
return x;

}

public static void main( String... args ) {
list.add("sales");
list.add("arglebargle");
list.add("cost of goods sold");
list.add("blarg");
System.out.println( contains() );
}

}
 
J

Joshua Cranmer

justineee said:
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.

Well, I don't know how you're generating your list, so I can't really
help you this much asides from this question:

Are you SURE that the ArrayList contains "sales" and not "sales ",
"sales\u0000", "sales\n", or some other string containing whitespace
and/or nonprintable characters?

Are you SURE that the ArrayList contains "cost of goods sold" and not
"cost", "of", "goods", and "sold"?
 
J

justineee

Well, I don't know how you're generating your list, so I can't really
help you this much asides from this question:

Are you SURE that the ArrayList contains "sales" and not "sales ",
"sales\u0000", "sales\n", or some other string containing whitespace
and/or nonprintable characters?

Are you SURE that the ArrayList contains "cost of goods sold" and not
"cost", "of", "goods", and "sold"?


I transfer the words from initlist.txt to the ArrayList using
FileReader.. Will this make any difference? Because when I show the
words they are all correct but not in the boolean method..
 
J

Joshua Cranmer

justineee said:
I transfer the words from initlist.txt to the ArrayList using
FileReader.. Will this make any difference? Because when I show the
words they are all correct but not in the boolean method..

Code, please?
 
L

Lew

My goodness, what an obfuscation! You don't need to compare a
'boolean' value to 'true' or 'false'; you can just use it directly.

public boolean contains()
{
return list.contains( "sales" )
&& list.contains( "cost of goods sold" );
}

With an expression that short, one wonders if wrapping it in a method
hides too much. So the client code could avoid the intermediate call
to 'contains()' altogether and just use the expression, so

if ( contains() )

becomes

if ( list.contains( "sales" )
&& list.contains( "cost of goods sold" ))
Please do not quote sigs.
I transfer the words from initlist.txt to the ArrayList using
FileReader.. Will this make any difference? Because when I show the
words they are all correct but not in the boolean method..

<http://sscce.org>

Come on, you've been around long enough to know that, absent an SSCCE,
the error is always in the code you don't show.
 
J

John B. Matthews

justineee said:
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>
 
L

Lew

I transfer the words from initlist.txt to the ArrayList using
FileReader.. Will this make any difference? Because when I show the
words they are all correct but not in the boolean method..

You didn't answer Joshua's questions. You should answer his
questions.

One period at the end of a sentence suffices.
 
A

Andreas Leitgeb

justineee said:
I transfer the words from initlist.txt to the ArrayList using
FileReader.. Will this make any difference? Because when I show the
words they are all correct but not in the boolean method..

While reading in the list from the file, write out each item followed
by a colon and the length of the item.

Just like Joshua, I suspect some unprintable characters to be hidden
in the strings - seeing the length and comparing that to a manual
character count surely helps to spot them.
e.g.: if you then see: sales:6 then you know there's some extra garbage
in the string, and if you instead see :6les then you have read a carriage
return from the file into the string.
 
J

justineee

Sorry for the mistake. I forgot to say that the I created the
ArrayList like:
ArrayList<AccountingClass> list = new ArrayList<AccountingClass>();

then this is the builder of AccountingClass

public AccountingClass(String accountName)
{
amount = 0;
this.accountName = accountName;
debitTransactions = new ArrayList<String>();
creditTransactions = new ArrayList<String>();
totalDebitAmount = 0;
totalCreditAmount = 0;
balance = 0;
}

Maybe my code is wrong because sales and cost of goods sold were typed
as String inside the parameter. I want to check whether if the list
contains sales and cost of goods sold. How will I do what I want to
do? What will I put inside there parameter of

list.contains( );
 
J

justineee

Nevermind I got it! THANKS TO YOU ALLL

Sorry Mr. Lew for not knowing how to use SSCCE. I have tried many
times. Maybe next time......... again.
 
J

John B. Matthews

[...]
if (list.contains("sales") && list.contains ("cost of goods sold")

Did it occur to you that the string containing "sales" in the test
could be a different string than the one containing "sales" that is
in the list?

Even if the strings have identical content, but, in fact, are two
different strings, list.contains (Object) will return 'false.'

Try iterating over the contents of 'list' and check each element for
"sales" or "cost of goods sold". You can use the String.equals
(Object) method.

I wondered about this, too, but the API suggests that contains() returns
a result consonant with equals(). For List<String>, I'd expect
String#equals() behavior:

<http://java.sun.com/javase/6/docs/api/java/util/List.html>
<http://java.sun.com/javase/6/docs/api/java/lang/String.html>

I suspect, along with you and others, that the OP's problem is related
to whitespace.
 
L

Lew

justineee said:
Nevermind I got it! THANKS TO YOU ALLL

What was the answer?

The best way to show your thanks is to share the results so that you
aren't the sole beneficiary of the insight.
Sorry Mr. Lew for not knowing how to use SSCCE. I have tried many
times. Maybe next time......... again.

Try reading this:
<http://sscce.org>

To know "how to use SSCCE", simply compile what you plan to post to
Usenet. If it compiles, you have an SSCCE. If it illustrates the
question you're asking, it really is an SSCCE.
 
R

Roedy Green

public boolean contains()
{
boolean x=false;

if (list.contains("sales")==true && list.contains("cost of goods
sold")==true)
{
x=true;
return x;
}
return x;

}

See http://mindprod.com/jgloss/newbie.html

== true is what I call "stuttering".

You could collapse that down to:

public boolean both()
{
return list.contains( "sales" )
&& list.contains( "cost of goods sold");
}

Perhaps you meant || instead of &&?

Perhaps the list does not contain "sales" and "cost of goods sold".

Write some debug code.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
R

Roedy Green

I transfer the words from initlist.txt to the ArrayList using
FileReader.. Will this make any difference? Because when I show the
words they are all correct but not in the boolean method..

Are you capable of making a mistake? of course! To find out dump out
the contents, surrounded in [ ] to help detect lead/trail space.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
R

Roedy Green

Sorry for the mistake. I forgot to say that the I created the
ArrayList like:

As a newbie, the error will nearly always be in the code you don't
post.

See http://mindprod.com/jgloss/ssccee.html

The whole reason you are having so much trouble is you are most likely
looking in the wrong place for the problem.

--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
R

Roedy Green

Sorry Mr. Lew for not knowing how to use SSCCE. I have tried many
times. Maybe next time......... again.

see http://mindprod.com/jgloss/ssccee.html

basically it means posting your entire code. In your case the problem
is unlikely too huge to post. If it were, the SSCCE requires you to
simply the code so that it still shows the problem, but is pruned down
to a reasonable size.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
R

Roedy Green

While reading in the list from the file, write out each item followed
by a colon and the length of the item.

it is often a good idea to use .trim() before adding anything into a
Collection. That will get rid of lead/trail control characters too.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
J

Jon Gómez

Lew said:
My goodness, what an obfuscation! You don't need to compare a
'boolean' value to 'true' or 'false'; you can just use it directly.

I remember seeing this in student code when I was a tutor during my
undergraduate studies at my University. It's interesting in that it
doesn't actually constitute an error, but that doesn't make it any less
awkward.

Jon.
 
C

Chris Riesbeck

Jon said:
I remember seeing this in student code when I was a tutor during my
undergraduate studies at my University. It's interesting in that it
doesn't actually constitute an error, but that doesn't make it any less
awkward.

I finally realized that the only way students could learn to avoid
things like this was if all code was critiqued (by human and/or machine)
for patterns like this. Testing won't reveal the problem.

The above combines several different common bad patterns.

The code that spurred me to critiquing student C++ code was this line
that cropped up in a several definitions of a templated version of
absolute value:

if (x < 0) return x - 2 * x; else return x;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top