简体   繁体   中英

Issue with formatting an output file

So I am currently having an issue with formatting with an output file that I create using an input file. The input file(file.txt) has this information with in it, exactly as seen here:

32 54 67.5 29 35 80 

115 44.5 100 65

I want to print the output file where the numbers are in a vertical line with a total of all the numbers added up at the bottom of the line. The code for doing this (that I'm trying to use) is -

         while(in.hasNextDouble()) 
         {
         double value = in.nextDouble();
         out.printf("%15.2f\n", value);
         total += value;
         }
         out.printf("Total: %8.2f\n", total);

It seems to print all the numbers on the same line instead of one after another. Am I missing a piece in the formatting? I can't seem to figure it out.

Use %n instead of /n ie

out.printf("%15.2f%n", value);

%n is platform independent newline character. Refer Oracle docs

/n may work sometimes, but not always with printf .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM