• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple if statement problem  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys!
I'm new to the forum, so this is my first post, but I'm excited to learn from you all, and later be able to give help as well!

So, I'm stuck at a spot in a basic sort of... if input statement thing. I don't know if I want to use a switch statement or an if, I'm just try to ask the user if he wants to know if his dog can jump over the fence, if the person says yes, then I just say "ok cool.", and if he says no, then it'll say "then what the hell do you wanna know?" And at the bottom, this is where I'm stuck, with the if and else part. In my main class, when I run it, if the user responds "no", then it says the right thing, but if I say "yes", then it gives me the "no" answer. =(
I hope I gave complete information. Thanks!

nlbas

[Added code tags - see UseCodeTags for details]
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian. Welcome to the Ranch!

The problem is that you're using == or != to compare Strings. This checks to see if they are the same object, not whether they have the same value. you should always use the equals() method to compare Strings.


We've got an FAQ with much more detail on this that you might find useful: AvoidTheEqualityOperator.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use switch-case for comparing Strings (from jdk7), it works similar to equals(). You may also use equalsIgnoreCase() (Java is case sensitive) if you want your output to be correct regardless of upper case or lower case letters.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin TrojanGupta wrote:You may also use equalsIgnoreCase() (Java is case sensitive)


You're kind of conflating two unrelated facts here.

1. The Java language is case-sensitive for reserved words and identifiers. This is what people generally mean when they say "Java is case-sensitive." So we can't use FOR or While for looping, and if we name our class Foo and our method bar, we can't use foo and Bar to refer to them.

2. The equals() method of the String class is case-sensitive. This has nothing to do with the Java language. It's just how that particular method was implemented because the designers of that class thought that's how its equals() semantics should work. (The decision to make is that way may have been influenced by #1 though.)
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even case‑insensitive language like Eiffel® usually treat Strings as case‑sensitive.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When comparing strings your do want to use the dogString.equals(myString) in your if statement. Also, I do not like that else in that question. The reason is because if I type something other than yes by accident then your user does not have a chance to correct their answer. You can use an else/if for the "no" comparison and then an else that can break loop; to the beginning of the question until the user answers correctly. Another solution would be a while loop that will not exit until the user answers correctly.

If you are just practicing, I would create two programs. One with the if statement and the other with the switch.

-Thanks for your question and post. Please let us know how it works out for you!
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if not to use equals to compare strings,
you can intern the input string

answer = mySkin.nextLine().intern();
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drac yang wrote:if not to use equals to compare strings,
you can intern the input string

answer = mySkin.nextLine().intern();

This is a bad idea. Always use equals() to compare strings. Although it works to intern strings and then use == or !=, do not do this. It's confusing and fills up permgen space (a part of Java's heap that never gets garbage collected).

It's an interesting trick, but a really bad idea for any serious piece of software.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drac yang wrote:if not to use equals to compare strings,
you can intern the input string

answer = mySkin.nextLine().intern();


I second Jesper's objection to that approach. Even if you do have a good reason to intern a String, you should never rely on the fact that a String is interened or that a boxed primitive is cached just so you can use == instead of equals(). Write code whose semantics matches those of your logic. In this case, those semantics are "Does the state of object A match the state of object B?" and the equals() method is exactly for that purpose, while the == operator is most definitely NOT for that purpose.
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I did my MSc “advanced Java” assignment, one of the chaps had come from a C# background. In C#, it appears the == operator is automatically overloaded when applied to Strings, to act like an equals() method (‍). But it worked in his Java app, because all the Strings were loaded from literals.
Later on, we had to persist the state of our objects, which I did by writing its state to a text file, and this chap did by serialising the entire application to a file. My app worked afterwards, and his didn’t. The deserialised Strings didn’t pass the test with == any more. Fortunately I knew enough to be able to explain the problem, and even why it appeared to work before serialising.
That was a problem quite similar to that of relying on Strings having been interned.
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, guys, for string comparison, equals method is sure to have more adaptivity, it's preferable, first of all, it's not restricted to the limited range of interned strings.
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:This is a bad idea. Always use equals() to compare strings. Although it works to intern strings and then use == or !=, do not do this. It's confusing and fills up permgen space (a part of Java's heap that never gets garbage collected).

It's an interesting trick, but a really bad idea for any serious piece of software.


yes, i agree, in fact i did think using equals is preferable in the first place.
just a topic extending. you did good job.

by default the permgen doesn't get GCed unless JVM options "CMSPermGenSweepingEnabled" and/or "CMSClassUnloadingEnabled" are turned on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!