Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem using DataInputStream Class

 
Mansoor Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I'm having problem using the DataInputStream class. When I enter a value using readInt() function of this class, I get a different value than what I entered through the console. I get a value in thousands when I simply enter 5.
Please help me out guys. Though there may be a simple fix to this but I really am confused.
Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the API documentation for that method?

Why are you using a DataInputStream rather than something like a BufferedInputStream for text input? Or even a Scanner?
 
Mansoor Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell, thanks for considering my problem. I've gone through the API Documentation and I'm using the method correctly still I'm getting an unexpected value. I have to teach this concept to class 12 students that is why I'm using the DataInputStream class. The simple code that I'm trying is :

int a=0;

DataInputStream dis=new DataInputStream(System.in);


System.out.print("Enter a number : ");
try{

a=dis.readInt();

} catch ( Exception e) { }

System.out.println("Value of a= "+a);



Now this is how the code respond :

Enter a number : 5
5

Value of a= 889861386



Can you help me out with this?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansoor Khan wrote:I've gone through the API Documentation and I'm using the method correctly

No; no you're not. readInt() expects the four bytes of a 32-bit two's complement machine word. It does not expect text. readInt() is appropriate for reading binary data files, or binary network protocols, but it's never appropriate for reading from the console. This is true of most of DataInputStream's methods.

The Javadoc for DataInput.readInt() (to which DataInputStream.readInt() refers you) says this:


Reads four input bytes and returns an int value. Let a-d be the first through fourth bytes read. The value returned is:

(((a & 0xff) << 24) | ((b & 0xff) << 16) |
((c & 0xff) << 8) | (d & 0xff))


This method is suitable for reading bytes written by the writeInt method of interface DataOutput.


It seems highly unlikely to me that you read this already, as Campell asked you to!
 
Mansoor Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest for showing concern. I did go through the API documentation but it seems I couldn't get it fully.
Actually this class(i.e. DataInputStream) along with BufferedReader class is in the syllabus of a senior secondary school and I've to teach this as I am a private tutor.
I wanted to use the DataInputStream class to read from the console since its in a way shorter to write than the character oriented BufferedReader class.
But it seems it's not possible, if I got you correctly.
Thank you for the clarification anyways.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic