File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to print Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to print "0"?" Watch "How to print "0"?" New topic
Author

How to print "0"?

zhong
Greenhorn

Joined: Mar 02, 2004
Posts: 1
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

Joined: Oct 02, 2003
Posts: 10908
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281


The nice thing about Standards is that there are so many to choose from!
 
 
subject: How to print "0"?
 
Similar Threads
single arrays in java
String to String array
A problem determining the length of an int variable converted to a string..........
Regex help
how to split a number into two multipliyers