Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-07-2009, 02:04 PM
Member
 
Join Date: Jan 2009
Posts: 1
Rep Power: 0
ankurdh is on a distinguished road
Default System.in.read() confusion
hello all.. i'm new to the community and as well to java. I hav good programming experience wit C and C++. the object oriented concepts wern a prob for me to understand. But i'm stuck wit the basic readin of a number from the keyboard!!!
i used the System.in.read(); statement. It read the number and returned the ascii code of the number. so for every System.in.read() shud a -48 be appended in the code???
that would make the task more complicated. so i wanted to know wot other ways are used in java to read numbers from the keyboard?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-07-2009, 02:09 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
If you were to read the API docs (JDK 6 Documentation) that method reads a byte. And since the input stream being read is, obviously, a "text" stream, that byte will be, of course, the character's byte value in the encoding that console is currently using (which may, or may not, be a standard ASCII value). Use the
Code:
new String(byte[])
constructor after reading and appending all the bytes you wish to read (or you could use the
Code:
read(byte[], offset, length)
method to read multiple bytes), then use the
Code:
Integer.valueOf(String)
method to get the actual integer value.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-07-2009, 02:12 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Of course, you can always wrap that input stream in an InputStreamReader and read the "string" directly (using readLine), rather than having to read the bytes and reconstruct the String, but the principle remains the same.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-08-2009, 03:23 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Its much better style to use InputStreamReader or BufferedInputReader and then read a line of text. Then call a parse function to convert the text to whatever you want.

Dealing with bytes and manually converting bytes to characters is (1) ugly C style and (2) makes it nearly impossible to properly handle international languages and character sets.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-08-2009, 09:18 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
And, for this sort of Stream, that is true, but not always. Only for "text" Streams, of course, and if the source of the Stream uses a different encoding than the systems default, don't forget to use one of the constructors that allows you to specify the encoding.

You definately do not want to use a Reader, of any kind, for, say, Image data, obviously. ;-)
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-08-2009, 09:28 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Originally Posted by masijade View Post
You definately do not want to use a Reader, of any kind, for, say, Image data, obviously. ;-)
Obviously. You have to know what kind of data is in the file in order to properly process it.

But if its character data, you want to use a buffered, line-oriented steam.

And unless you are a guru, in which case you should not be posting in the "new to java" forum, you should not be reading binary, rather use a library that does it all properly.

Java has libraries for nearly everything. music, audio, images, movies, etc. Do not reinvent the wheel. Go for code reuse.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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
bracket confusion...? gallimaufry New To Java 4 10-28-2008 12:17 PM
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected kushagra New To Java 5 10-17-2008 03:13 PM
Please Read!!! jeffranc New To Java 0 08-21-2008 09:47 PM
System.in.read(); strange error! kantze New To Java 2 03-19-2008 02:44 PM
How to read the following rrp New To Java 0 12-03-2007 07:16 PM


All times are GMT +2. The time now is 05:16 PM.



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