Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 339
Stan Avatar asked Dec 03 '22 02:12

Stan


2 Answers

More specifically, I would recommend using:

x.format("%d%n%s%n", m.getPoints(), m.getStoreItem1Bought());
like image 183
Nathan Ryan Avatar answered Dec 19 '22 11:12

Nathan Ryan


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
like image 27
Aleadam Avatar answered Dec 19 '22 11:12

Aleadam