The moose likes Beginning Java and the fly likes Problem using DataInputStream Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem using DataInputStream Class" Watch "Problem using DataInputStream Class" New topic

Problem using DataInputStream Class

Mansoor Khan

Joined: Apr 14, 2008
Posts: 17
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.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38007
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

Joined: Apr 14, 2008
Posts: 17
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 : ");


} catch ( Exception e) { }

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

Now this is how the code respond :

Enter a number : 5

Value of a= 889861386

Can you help me out with this?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

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!

[Jess in Action][AskingGoodQuestions]
Mansoor Khan

Joined: Apr 14, 2008
Posts: 17
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.
subject: Problem using DataInputStream Class
Similar Threads
How does inheritance work
array of object to call a method
how to redirect the linux terminal contents to a textpane
looping problem with break and continue
won't let me input integer values