• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 48
Eclipse IDE Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use this as well.

 
Marshal
Posts: 74712
335
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Varun

It works
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic