• Post Reply Bookmark Topic Watch Topic
  • New Topic

DataInputStream  RSS feed

 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i am confused after the behaviour of this code. as I know the readInt() gives primitive int value of DataInputStream. my code is : and as soon as i give command java Test on cmd : it asks for a number , i give it..... but .. it again wait for more input.. when i give it another number : an int value is printing , which came from where ?.
Thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use DataInputStream to read from System.in.

DataInputStream expects binary data, in a specific format. System.in doesn't provide data in the format that DataInputStream expects.

The easiest way to read a number from the keyboard is by using class java.util.Scanner.
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir , its ok to use Scanner for this purpose , but if this code is running .... then I want to know it's behaviour... from where that int was coming please
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read about the DataInputStream#readInt() method and the DataInput interface? That will explain exactly what Jesper told you earlier.

When you do, I hope you will stop using data input streams for text input.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!