• Post Reply Bookmark Topic Watch Topic
  • New Topic

New Line in Text File  RSS feed

 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys. I'm trying to print the information entered into my TimeClockApp to a text file named timeclock.txt. Everything is writing to the text file, except instead of adding each part of the run through the app to one line, it is saving each part on a separate line like this:

To ensure that each run through the time clock is recorded on a different line in the text file, I want it to add each item like this instead:

I have tried to execute the out.println(timeClock.get(i)); line as out.println(timeClock.get(i) + "\t"); and out.println(timeClock.get(i) + "\n");, but have had no luck in fixing this problem. My current code that writes the information to the text file is as follows:

Thanks in advance for your help. I'm not very good at Java yet!
 
Jacob Draper
Ranch Hand
Posts: 35
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out.println will output and move to next line,
out.print will keep you on the same line.

Remove "ln"
 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Draper wrote:Out.println will output and move to next line,
out.print will keep you on the same line.

Remove "ln"
Thank you, Jacob! That almost works completely. Everything is now in one line, but it seems that the second time I go through the TimeClockApp, the next record is recorded next to the previous record (on the same line) instead of on the next line. Like this:

Any tips for recording each line on its own line like this?:

Thanks again!
 
Jacob Draper
Ranch Hand
Posts: 35
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add


or you could do


Add one of those where necessary. (I believe it would go outside of your for loop. Probably after it?


So it would look like



not 100% that is the location for it, but you are gonna need to put it in the right place to move you to the next line.
 
Anakela Bella
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Draper wrote:You can add


or you could do


Add one of those where necessary. (I believe it would go outside of your for loop. Probably after it?


So it would look like



not 100% that is the location for it, but you are gonna need to put it in the right place to move you to the next line.

Ooh, that's what I was doing wrong. I was putting out.println(); inside the for loop, so I wasn't getting the correct result.

Thank you! I placed out.println(); outside the for loop, and now things are recording correctly. Thanks again, Jacob!
 
Jacob Draper
Ranch Hand
Posts: 35
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad I could help!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Draper wrote:or you could do

I know this is nitpicking but that may or may not do the same thing as using println() depending on which OS you are using.

As the API docs for println() say:
"Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n')."
 
Jacob Draper
Ranch Hand
Posts: 35
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is nitpicking but that may or may not do the same thing as using println() depending on which OS you are using.

As the API docs for println() say:
"Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n')."


I did not know that. Thanks!
It wouldn't make much sense to use a plain string with a \n when it could be done simply with println, but I thought I'd offer options. Good point!
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you should not use \n. If you use println/print, I suggest you change to printf and use the %n tag. You can read about it in the Java Tutorials by looking for formatting. I think there are two hits: 1 2.
I know you will find lots of books with \n in, but that only gives you the correct line end on a Unix‑like operating system. Windows uses \r\n
 
Jacob Draper
Ranch Hand
Posts: 35
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought Java was supposed to be "Write once, run anywhere".


Anyway, thanks for the links, I will look into that!
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java® is indeed write one run anywhere. But using \n ties you to a particular operating system. That is why it has line.separator and println and %n. If you use those, you break the link to any operating system.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!