• Post Reply Bookmark Topic Watch Topic
  • New Topic

couldn't get back integer value  RSS feed

 
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi , i have a problem which i couldn't get back integer user keyin input, anyone please guide me ...

the result display 51 rather than user integer input
tq
 
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the docs, what does read() claim to return?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alvin,
You mention that the output is 51, but you don't mention the input. I'll assume you input 3.
Note that the character '3' is ASCII and Unicode character number 51. Does this hint give you an idea of what the read method might be returning?
Note that the documentation mentioned by Stefan is located at
http://java.sun.com/j2se/1.4.2/docs/api/
If it's not clear yet, the thing to understand is that the read method is returning the value of the input character, and that the value of the character '3' is 51, '4' is 52, etc. (A nice reference to have around at times is http://asciitable.com for such character information.)
If you were to alter your final output to cast the input to a char, you might see what you were expecting in the first place.
System.out.println("num: " + (char)a);
Also, note that you might at times prefer to use a BufferedReader, which is capable of reading in an entire line of input as a String.
[ March 31, 2004: Message edited by: Dirk Schreckmann ]
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, my input is 3 , all i want is to display 3 ....as mentioned by Dirk, it is ascii code ..fine , but how i get back 3 rather than 51 ? means how i convert ascii code to integer input that user keyin ?
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ...i get it ...thks dirk and stefan ....need to cast it to char ... i get
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry....problem again
refer to program , if i wan variable "a" become non-ascii code number for processing purpose as parameter or value for addtional subtraction and so like , what should i do ?
is that possible i can parse it to NON-ascii number "3" in this case ?
tq
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!