Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-19-2008, 01:24 PM
Member
 
Join Date: Mar 2008
Posts: 1
kantze is on a distinguished road
System.in.read(); strange error!
Hi, i'm new to java. I'm trying to read an integer and to print it.

Here is what i've done so far...
Code:
import java.io.IOException; public class helloworld { public static void main(String[] args) throws IOException { int a = System.in.read(); System.out.println(a); } }
The problem is, that if for example i write 3 the console prints 51 ( its' ansi representation, i guess??)

I must write this
Code:
System.out.println( (char)a);
to make it work.

But, when I do this
Code:
import java.io.IOException; public class helloworld { public static void main(String[] args) throws IOException { int a = 3; System.out.println(a); } }
everything is printed right.

Could anyone tell me what's happening??
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-19-2008, 02:41 PM
Member
 
Join Date: Mar 2008
Posts: 3
Rajesh is on a distinguished road
System.in.read() method reads the next byte of data from inputstream. The Value byte is returned as an int in the range 0 to 255. If no byte is available the value -1 is returned.

But in the code fragment int a=3; we are assinging integer value to position allocated for integer variable a. So it creates no problem.

hope is usefull,
thank you
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-19-2008, 02:44 PM
Member
 
Join Date: Mar 2008
Posts: 3
Rajesh is on a distinguished road
Instead you may try with this simple code to read an integer from console


import java.io.DataInputStream;
public class demo
{
public static void main(String s[])
{
int a;
DataInputStream dis = new DataInputStream(System.in);
a=Integer.parseInt(dis.readLine());
System.out.println(a);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help on a strange file access prob squad Advanced Java 0 03-21-2008 11:55 AM
How to read the following rrp New To Java 0 12-03-2007 07:16 PM
Strange error message "Source not found" ppayal Eclipse 0 11-25-2007 07:19 PM
ERROR: Java(TM) failes to apply changes to your system coco New To Java 1 08-07-2007 08:40 AM
read txt file sureshsri1981 JavaServer Pages (JSP) and JSTL 0 08-05-2007 04:49 PM


All times are GMT +3. The time now is 07:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org