• Post Reply Bookmark Topic Watch Topic
  • New Topic

check for newline

 
Sony Chowdhury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12342
39
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an actual question here?
 
Sony Chowdhury
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12342
39
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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]
 
Tony Docherty
Saloon Keeper
Posts: 3142
72
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!