Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with if/else

 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone explain why this is ALWAYS returning "ok"?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read() returns a single character. Simplifying a bit, the returned value is the Unicode value for the character, which (for the US/English locale) is the same as the ASCII code. ASCII maps characters onto numbers. The digits 0, 1, 2... are 48, 49, 50... decimal. Since these are all less than 100, you're always going to get "OK". Try typing a lower-case letter "z" -- the code for that is over 100, and so you won't see the "OK".

You need to translate characters into numbers. Read this to start learning the right way to do it.
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, reading the method summary I overlooked the fact it takes ONE character. Is there a way to read in entire ints using BufferedReader or do I just have to use Scanner?
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are static methods in either String, Integer or Double or something that do the reqired translation, and can handle input strings longer than one chararcter. This is called parse/parsing in computer terminology.



There's one from the Integer class. Every time I begin this problem, I have to look it up. That is what the experienced people do, look it up almost eveery time.

It is a normal problem, but real soon you will get lost in all the information. Just keep coding, compiling and fixing.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If you want to have some beginner fun, look at that one that says:

parseInt("Kona", 27) returns 411787
[ September 22, 2007: Message edited by: Nicholas Jordan ]
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic