Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Input Output If" Watch "Input Output If" New topic
Author

Input Output If

Daniel Lewis
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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
 
jQuery in Action, 2nd edition
 
subject: Input Output If