\n won't work, not going to a new line

34,587

Solution 1

More specifically, I would recommend using:

x.format("%d%n%s%n", m.getPoints(), m.getStoreItem1Bought());

Solution 2

Use this instead:

public static String newline = System.getProperty("line.separator");

Both of this options work. Your problem is in how you format the output:

System.out.format("%s" + newline + "%s" + newline, "test1", "test2");
System.out.format("%s%n%s", "test1", "test2");

Output:

test1
test2
test1
test2

Solution 3

Try using %n instead of \n when using format. For details on this, please see the Formatter API and search this page for "line separator" and you'll see.

Solution 4

No problem here:

System.out.format ("first: %s%s", "" + x + "\n", "" + y + "\n");

While I would prefere, to integrate the \n into the format String, not the values:

System.out.format ("second: %s\n%s\n", x, y);

Using Formatter.format works the same.

Share:
34,587
Stan
Author by

Stan

Slowly learning!

Updated on March 24, 2020

Comments

  • Stan
    Stan about 4 years

    I'm creating a small program that saves a int value into a text file, saves it, and loads it when you start the program again. Now, I need 3 more booleans to be stored in the text file, I am writing things in the file with

        public Formatter x;
    
    x.format("%s", "" + m.getPoints() + "\n");
    

    Whenever I want to go to a new line in my text file, with \n, it wont go to a new line, it will just write it directly behind the int value. I tried doing both

            x.format("%s", "" + m.getPoints() + "\n");
        x.format("%s", "" + m.getStoreItem1Bought() + "\n");
    

    and

        x.format("%s%s", "" + m.getPoints() + "\n", "" + m.getBought() + "\n");
    

    but, both will just write the boolean directly behind the int value, without starting a new line. Any help on this?

    I am using Windows 7 Ultimate 64 bits, and my text editor is Eclipse, I am running all of the code with Eclipse too.

  • Stan
    Stan about 13 years
    That outputted 3nullfalsenull into my text file, didn't work either.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels about 13 years
    My bad. I thought you were using a variant of the Formatter class such as is used with PrintStream's printf method.
  • maaartinus
    maaartinus about 13 years
    This works only for %n in the format string part of the Formatter.format method, not in the arguments.
  • Nathan Ryan
    Nathan Ryan about 13 years
    Even better: x.format((Local)(null), "%d%n%s%n", m.getPoints(), m.getStoreItem1Bought());. The first null argument will prevent any localization from being applied.
  • maaartinus
    maaartinus about 13 years
    This really should work. Unless you're doing the output in the class initialization before newline gets assigned.
  • Stan
    Stan about 13 years
    This worked, placed it on a new line, thanks for all your help guys.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels about 13 years
    @maaartinus: yep. I'm going to delete this answer soon and have up-voted the other better answers.
  • maaartinus
    maaartinus about 13 years
    I do NOT think your answer should be deleted! It's the most sane way for dealing with the newline insanity (besides completely ignoring it, which is not always applicable). My comment was just a usage note to the OP, not a critique of your answer.
  • Voo
    Voo about 13 years
    Don't use \n - that only works reliably on Linux systems (and modern Macs). %n or System.getProperty("line.separator") is the correct version. No need to write unportable code if the solution is hardly more work.
  • user unknown
    user unknown about 13 years
    Depends on what you like to do with the product. If you need a "\n", use a "\n", if you need a portable output, use "%n". Modern editors (except Notepad, which isn't modern) often can handle both forms of input.