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