This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Input Output If Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Input Output If" Watch "Input Output If" New topic

Input Output If

Daniel Lewis

Joined: Dec 20, 2011
Posts: 10
Hello again!
I just typed the following simple program which gets an userinput to the variable inData. Then the input is checked for having typed "x" or not. For me, the if statement should work, but the program always says "You have not typed x!" even when typing x.

Cannot figure out the problem...
Rob Spoor

Joined: Oct 27, 2005
Posts: 20136

Use !inData.equals("x") and inData.equals("x") instead of inData != "x" and inData == "x". Strings are objects, and with != and == you're comparing references, not String contents. That's what equals does.

How To Ask Questions How To Answer Questions
Ifteqar Ahmed
Ranch Hand

Joined: May 02, 2011
Posts: 85

String.equals("another string") method checks the contents of the string not the location of the string in the string pool.

So you should always use string.equals() method for comparing ontents
I agree. Here's the link:
subject: Input Output If
It's not a secret anymore!