• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Input Output If

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 85
MySQL Database Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic