0

Can you tell me is there other ways to put a new line between System.out.printf - standard output functions I hope it is relevant to ask

  package formattedoutput;

  public class FormattedOutput {

    public static void main(String[] args) {

        double  amount;
        amount = 43.676;
        int spaces;
        spaces = 77;


        System.out.printf( "%1.2f", amount );// the "2" in 1.2 specifies the number of digits to use after the decimal point. 
        System.out.println();
        System.out.printf("%12d", spaces ); // specifies the minimum number of spaces that should be used for the output. If the integer that is being output takes up fewer than 12 spaces, extra blank spaces are added in front of the integer to bring the total up to 12.
    }

  }
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • As you see in my case I use formatted output that's why when I put it at the end of format string I get error... –  Feb 14 '14 at 22:59
  • You're not putting it at the end of the format string, then. Perhaps you're putting in an extra parameter instead of adding the `%n` to the first string. – chrylis -cautiouslyoptimistic- Feb 14 '14 at 23:03

1 Answers1

3

You probably just want to add %n to the end of your format string.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • I am trying to put it right after amount before closing brace NetBeans gives me error ... Can you show me how would you put please? in this case formatted output is used which in double quotes and %n should be placed inside them –  Feb 14 '14 at 22:57
  • @Serenity At the end *of the format string*: `"%1.2f%n"`. – chrylis -cautiouslyoptimistic- Feb 14 '14 at 23:02
  • 1
    @Serenity I don't believe you. Post your code. – chrylis -cautiouslyoptimistic- Feb 14 '14 at 23:04
  • It is ok if you don't believe but why would I lie I am here to find an answer ... give me one sec please... Can I post a screenshot of it ? –  Feb 14 '14 at 23:05
  • @Serenity Please don't post screenshots; they're hard to read and can't be searched. Just copy and paste your current code into a new box in your question; there should be an "edit" link at the bottom of it. (By the way: *I don't believe you* in this case doesn't mean quite that I think you're lying; it means that I don't think you followed my instructions exactly, and that's why it's not working.) – chrylis -cautiouslyoptimistic- Feb 14 '14 at 23:07
  • @Serenity In the future, please put updated code like that in your question by editing it, not by writing a comment. In this case, you added an extraneous `String` before your `System.out.printf` call, and you should have gotten an error pointing right there. Removing it results in code that's working perfectly. – chrylis -cautiouslyoptimistic- Feb 14 '14 at 23:10
  • I realized that let me delete it –  Feb 14 '14 at 23:11
  • I can not exactly show you code with error even if I edit my current code in the box above you would not see that how can I show you that ?Are there any suggestions? –  Feb 14 '14 at 23:12
  • @Serenity Click "edit" under your original question. The editor will pop back up. You can then paste your new code at the bottom and format it, and you can save the new version. As I mentioned, though, if you take out the extra `String` in front of your print statements, the code you posted works. – chrylis -cautiouslyoptimistic- Feb 14 '14 at 23:13
  • Oh you were right didn't even know that 'String' was causing it I was just about to add string value for formatted output ... Thank you –  Feb 14 '14 at 23:15