File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to print "0"?

 
zhong
Greenhorn
Posts: 1
  • 0
  • 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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • 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: 281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic