It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Problem with if/else Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with if/else" Watch "Problem with if/else" New topic
Author

Problem with if/else

Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 199
can someone explain why this is ALWAYS returning "ok"?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 199
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

Joined: Sep 17, 2006
Posts: 1282
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.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282


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

Joined: Aug 12, 2004
Posts: 199
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with if/else
 
Similar Threads
Scanner class help
Help with reprinting downloaded text from FTP site onto GUI!
Java grid colouring in game
How to run .java file from Runnable
Temperature program