Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with comparing strings

 
Taylor Bundy
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Tom Reilly
Rancher
Posts: 618
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch :-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic