Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Input Output If

 
Daniel Lewis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ifteqar Ahmed
Ranch Hand
Posts: 85
Java Linux MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic