0

I'm fiddling with a basic code that utilizes methods to create values, and I want it to write into a text file and format the values, however the \n in my printf statements will not actually indent the solution.

File surfaceG = new File("SurfaceGravity.txt");
surfaceG.createNewFile();
PrintWriter write = new PrintWriter(surfaceG);
String placer = "";
write.printf("%5sPlanetary Data\n", placer);
write.printf("%10sPlanet, placer);
write.close();

The solution I'm aiming for is

     Planetary Data
         Planet

However the solution this code will give is

Planetary Data Planet

Why won't the \n in my printf statements compute through to the textfile?

  • Its working fine.one small mistake is missing quote. write.printf("%10sPlanet, placer); – Sanjay Mar 14 '19 at 15:25
  • You're using Windows, aren't you? The `"\n"` character is only a newline on Linux-like platforms. Use "%n" in a System.out.printf instead of `"\n"` in order to be platform-independent. – Klitos Kyriacou Mar 14 '19 at 15:30
  • @KlitosKyriacou Very helpful, honestly wracking my brain – Jordan L Mar 14 '19 at 19:55

0 Answers0