• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

check for newline

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wap that takes an input from a text file and replaces a * by a # and every new line by / and stores the output to another file
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there an actual question here?
 
Sony Chowdhury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes sir its a question i was not able to slove this about how to check for newline
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This site doesn't usually just hand out answers. You might want to read some of this. In particular, the Show Some Effort and Tell The Details.

Post your code so far. Tell us what you've tried. What exactly does your code do (or not do) that it should (or shouldn't)?

You job, when posting question here, is to make it as easy as possible for us to help you, thus increasing the chances of you actually getting that help.
 
Sony Chowdhury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir the program should replace * into # and newline by / in a file
IMG_20160506_235424.jpg
[Thumbnail for IMG_20160506_235424.jpg]
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A photo of your code isn't an acceptable (or useful) way of showing your code. Please cut and paste the code into your post (and wrap it in code tags ) so we can easily read the code and best advise on how to get it to do what you require.
 
Sony Chowdhury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the newline character is represented by "\n" in Java, not by "/n". But you've got another problem after that one, don't you? The way you quote the requirements, they don't explicitly say that all of the other characters should be copied unchanged from the input to the output. So maybe that isn't a requirement?
 
Sony Chowdhury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that was my typing mistake but its not working
 
Sony Chowdhury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not the proper way to read a file, and the error is in the handling of b. First of all, you check if it's -1 after you've written it to the output stream. Secondly, InputStream.read() returns an int value from -1 to 255 (inclusive). Everything but -1 indicates a valid byte. However, if you cast it to a byte, its range is changed to -128 to 127 (inclusive). -1 is in there twice -- once as -1 cast to byte, and once as 255 cast to byte. That's why the method returns an int and not a byte.

Here's how you should properly read a file:
Or a bit shorter:

I'd also like to advise you to:
1) Use a Reader instead of an InputStream. Your code will only properly handle ASCII files.
2) Use a BufferedReader (or BufferedInputStream) to improve performance.
3) Close your streams when you're done with them (in a try-finally construct, or preferably try-with-resources).
reply
    Bookmark Topic Watch Topic
  • New Topic