aspose file tools*
The moose likes Beginning Java and the fly likes Trouble with comparing strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with comparing strings" Watch "Trouble with comparing strings" New topic
Author

Trouble with comparing strings

Taylor Bundy
Greenhorn

Joined: Feb 24, 2011
Posts: 2
Hi guys, I'm studying Java at my universtiy, and I'm working on a project. Now, here's my problem! :p

So I'm making a tax calculator, here's my code:




I'm trying to have the method statusCheck compare the entered information against the constants single, head, joint, and seperate.
Well, when I go to compile, I get the error:
TaxCalculator.java:16: operator || cannot be applied to boolean,java.lang.String
if (status != seperate || joint || head || single) {

What's my problem here?
Thanks!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Two problems :
1. You are comaring strings using ==/!=. Don't. Compare strings using equals()
2. Strings are not booleans. When you write "|| joint ||", the compiler expects a boolean(true/false) value here. What do you want to express exactly ?

[My Blog]
All roads lead to JavaRanch
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I assume you want to compare status with seperate and status with joint and status with head and status with single. I see two problems.

1. In Java, you cannot specify the code as you would in English: if status is not equal to seperate or joint or head or single. You need to code it as in English: if status is not equal to seperate and status is not equal to joint and status is not equal to head and status is not equal to single.

2. You should not use "==" when comparing two Strings. You should use the equals() method.

Please respond if you need more guidance.
Taylor Bundy
Greenhorn

Joined: Feb 24, 2011
Posts: 2
Hey guys, thanks for the quick help.

As far as using equals(), I don't think we've been taught that in class. What I'm trying to accomplish here is, if the user inputs anything other than S, H, MJ, or MS, I want to have the program exit.
Problem is, it's difficult to compare strings, at least as far as I know how. I'm not sure how to use equals() at all.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

As far as using equals(), I don't think we've been taught that in class.

You want to be number one in your class ? Then read this article. You'll know what strings are and how to compare them. You'll understand how == differs from equals(). You'll say to your classmates "what ? you haven't read that awesome Javaranch article yet ?". Go ahead and read it carefully.

What I'm trying to accomplish here is, if the user inputs anything other than S, H, MJ, or MS, I want to have the program exit.

Then you have to compare each string, the same way you did for "seperate". Also, think again about using ||. Are you sure you want to "or", instead of "and" ?
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
And welcome to JavaRanch :-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Code looks a lot better if you use the code button, not "quote". I have changed it and you can see how much nicer it is.

And welcome again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with comparing strings