roro
roro

Reputation: 723

Not the correct input

I want to write some data to a file and here is my code:

for (Season s : seasons) {
            if (s.getYear() >= start && s.getYear() <= end) {
                line += s.getYear() + ", " + s.getWinTeamName() + ", "
                        + s.getMinorPremiersTeam() + ", "
                        + s.getWoodenSpoonTeam()+ "\n"; }
}
System.out.println("File created.\n"); // Informing the user about
// the creation of the file.
out.write(line);
out.close();

My file is created but this is what I get:

2005, Wests Tigers, Parramatta, Newcastle2006, Broncos, Storm, Rabbitohs

But I want it to be:

2005, Wests Tigers, Parramatta, Newcastle
2006, Broncos, Storm, Rabbitohs

I thought the adding the "\n" at the end of the line variable would fix that but it hasn't.

Any suggestions on how to fix that?

Upvotes: 1

Views: 73

Answers (3)

roro
roro

Reputation: 723

for (Season s : seasons) {
        if (s.getYear() >= start && s.getYear() <= end) {
            line += s.getYear() + ", " + s.getWinTeamName() + ", "
                    + s.getMinorPremiersTeam() + ", "
                    + s.getWoodenSpoonTeam()+ "\r\n"; }
}

System.out.println("File created.\n"); // Informing the user about
                                      // the creation of the file.
out.write(line);
out.close();

Upvotes: 0

Simon Curd
Simon Curd

Reputation: 850

If you use the java.io.BufferedWriter, the .newLine() method will insert the appropriate line separator.

for (Season s : seasons) 
{
   if (s.getYear() >= start && s.getYear() <= end)
   {
      String line = s.getYear() + ", " + s.getWinTeamName() + ", "
                        + s.getMinorPremiersTeam() + ", "
                        + s.getWoodenSpoonTeam();
      out.write(line);
      out.newLine();
   }
}

Upvotes: 1

BambooleanLogic
BambooleanLogic

Reputation: 8171

You need to use the system-specific newline separator. See System.lineSeparator

Upvotes: 3

Related Questions