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 Problem using DataInputStream Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem using DataInputStream Class" Watch "Problem using DataInputStream Class" New topic
Author

Problem using DataInputStream Class

Mansoor Khan
Greenhorn

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.
Thanks


kingmansoor
Campbell Ritchie
Sheriff

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

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 : ");
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
Greenhorn

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem using DataInputStream Class
 
Similar Threads
how to redirect the linux terminal contents to a textpane
looping problem with break and continue
won't let me input integer values
How does inheritance work
array of object to call a method