• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write input read from System.in to a Output Stream  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a code in which I am reading input from System.in and Destination is some where else

Here is my code



in the line while((a=c.read())!=-1)

a compile time error is shown "cannot invoke read on primitive data type int"


Where I am going wrong?


Thanks
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 10 you have declared the variable "c" as an int. int is a primitive data type which does not have methods that can be invoked. It is just a number that was assigned to it.

On line 8 you have already read the user input as a String. Then on line 10 you parse the String to an int and then assign that int to the variable "c". Now all you have to do is to use the variable "c" in the while loop. No need to do any extra read() or assignment, Just ( c != -1 ) and your done.

With what I mentioned above, the variable "a" is not needed at all.
 
Varun Selva
Ranch Hand
Posts: 48
Eclipse IDE Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use this as well.

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of you are making the serious mistake of not closing the readers and writers. you should use a finally statement to close them or (better) the Java7 feature of try with resources.

Then the flush() call becomes unnecessary; look at what it says about BufferedWriter#close.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Varun

It works
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!