Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to print "0"?

 
zhong
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a beginner in Java.I have found this code on BBS,the man said:this code's output is"48",if I key in"0".Why? Is it because that "int num" is transformed to "String num"???
Then,how can I print "0"?
Thanks.

-----------------------------------------------------
code:
import java.io.*;
public class first
{
public static void main(String[] args)
{
int num= 0;
System.out.print("input:");
try
{
num = System.in.read();
}
catch(Exception e){}
System.out.println("the input is:"+num);
}
}
[ March 23, 2004: Message edited by: tsai zhong-ling ]
[ March 23, 2004: Message edited by: tsai zhong-ling ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when someone types in 0 at the prompt, they are typing in the CHARACTER '0', not the integer value of 0. since you are working in an ascii world, '0' is encoded as 48, stored in your int, then printed out.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic