• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to change line after spaces while writing in file. I found it difficult to do. can anyone help??  RSS feed

 
yogesh raipure
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here in this part of code i am able to write the string source in file named Details but its written in a single line. Actually i want to write it such that it should be written in new line after every two spaces. i mean after center.getText it should change line and id.getText should be written in new line. this should go on till there is enf of file. also i want to append this data to the previous one written there in file. Thanks in advance....

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to build it that way. Right now, on that hideously ugly first line, you concatenate all the pieces together as one long string. Then you write that one long string to the file.

You will need to instead parse the data you are getting, figure out where to break it, and insert the breaks accordingly.
 
yogesh raipure
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still i could not figure out wat exactly to be done... i dont know about parsing it directly. can anybody give a bit mor details. Being very new to this field i need to know more for implementing any new thing...
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try a java.util.Scanner which can read individual tokens from a text file. Then you can simply count the tokens.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no magic here. If you want a new line after every two spaces, you need to put one in. If you want a new line between what center.getText() returns and what id.getText() returns, you need to put one in. That can be as simple as:

or you could format it in your try block:


Finally, if you want to insert newlines into what id.getText() returns, you will need to parse it. There are many ways this could be done. You might want to search for some kind of string replace method...you could pass in " " as what you want to change, and " \n" as what you want to replace it with.

That may or may not work, depending on your specific requirements - i.e. what if there are four or twelve spaces somewhere?

Note: the above code was not tested, and may not be 100% correct, but it simply to illustrate some options.

 
yogesh raipure
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried "\n" but that shows as some thing unrecognized thing while writing in file.. also that dont write tokens on every new line.. there might be some other way.. anyways thanks for your suggestion..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"some unrecognized thing" is a meaningless phrase. Nobody here has any idea what that means. Nor do we know what "I tried \n" means...tried it HOW?

"that do(esn't) write tokens on every line" - again...nobody can help you unless you tell us what you want - exactly. An example HELPS, but isn't everything needed. Show us your data. Show us the actual code you are running. show us the actual output you get. explain in DETAIL what you want.

Otherwise, nobody can help.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread appears to be a duplicate of http://www.coderanch.com/t/580504/java/java/you-help-me-code.

I'm picking this one to lock arbitrarily, though both have valid responses. (Other mods, feel free to overrule me.)
[Fred locked the other one, so I unlocked this one.]

Yogesh, in the future, please UseOneThreadPerQuestion.(⇐click)
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So many books tell you to use the LF character (\n) and so many books are wrong. The line end varies between operating systems (Windows® = \r\n = CR‑LF, Unix, Linux and Macs = \n = LF, old Macs, probably now obsolete = \r = CR). It also depends which application you use to display the text; many text editors ask what the line end is, but Microsoft NotePad assumes it has Windows line ends and can’t display other sorts correctly.
As somebody (?Bill Brogden) told you elsewhere, you want to find the correct line end. You can find it from the properties using something like this. . . and you can now use it after the + operator, or with the append method of a StringBuilder.
You can use a method of the PrintStream class, which Jeanne Boyarsky told you about.
You can put Strings together with String#format and use the %n tag (percent sign, not \).
You can use a Formatter and again use the %n tag; I think String#format uses a Formatter internally.
You can use a method of the BufferedWriter class which moves to a new line in the output file.
All those methods (if spelt correctly) will give the correct line end sequence for your OS.

You can take note of what people said about the first line of your code on another thread. I think your problem is that you are putting all the text into one line; if you had put the new line sequences in before you join it all as one line (as Bill Brogden[??] showed you), you would have had no problems now
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So many books tell you to use the LF character (\n) and so many books are wrong. The line end varies between operating systems (Windows® = \r\n = CR‑LF, Unix, Linux and Macs = \n = LF, old Macs, probably now obsolete = \r = CR). It also depends which application you use to display the text; many text editors ask what the line end is, but Microsoft NotePad assumes it has Windows line ends and can’t display other sorts correctly.


And of course in HTML you need < br / > . (Or < p >...< /p > depending on what you're actually trying to do.) And I have no idea how you force a newline in a Swing component.

The point being: You need to be aware of the context in which your text is being displayed if you want to control the formatting.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So many books tell you to use the LF character (\n) and so many books are wrong.

I knew you would come in a correct me. I was trying to right something quick that would PROBABLY be ok...I thought "I should note that Campbell will probably come along and correct my \n's", but then forgot before I hit 'submit'.

oh well...
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . but then forgot before I hit 'submit'. . .
You mean the way I forget that Rob Spoor was there 5 seconds ago, before I hit “submit”?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I am a pedantic so‑and‑so, but many applications will actually compensate for the “wrong” line end. NotePad® is one that won’t.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I know I am a pedantic so‑and‑so, but many applications will actually compensate for the “wrong” line end. NotePad® is one that won’t.

The easiest workaround I've found is to copy the content to Word and back.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the hardest workaround? Obviously Word® can compensate for incorrect line ends.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hardest workaround is moving to each line end and pressing <Enter>

Done that too
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you have to go to the end of the line and push <del><enter> to avoid duplicate line ends.

Yes, I have done that, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!