0

I've written a program in java "CircleDemo" that inevitably does this:

System.out.printf("The circle's area \t   is \t %.2f \n", circle.getArea());
System.out.printf("The circle's diameter \t   is \t %.2f \n", circle.getDiameter());
System.out.printf("The circle's circumference is \t %.2f \n", circle.getCircumference());

In cmd.exe the display looks like this:

The circle's area          is    3.14 
The circle's diameter      is    2.00
The circle's circumference is    6.28

Nice, clean formatting.

However, Notepad++'s console (nppexec) prints the same program like this:

The circle's area     is    3.14 
The circle's diameter      is    2.00
The circle's circumference is    6.28

You can see how the formatting is different. Now, I've played around long enough to find what's causing this is that "\t" prints tabs differently in cmd.exe vs Notepad++'s nppexec.

How could I edit nppexec's "\t" formatting to print "\t" the same as cmd.exe would?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Mark Puchala II
  • 634
  • 2
  • 8
  • 25
  • 1
    We use `printf` to avoid `\t` and its problems. You should always specify precisely how many spaces you need. Using `\t` to achieve array formatting is rarely good idea. – Pshemo Jul 03 '15 at 15:44
  • Thank you, @Pshemo, for helping better format my question. I didn't know I could display code so neatly using this "4 spaces" technique. – Mark Puchala II Jul 03 '15 at 16:47
  • No problem, and it is not that it is "4 spaces technique", but `{}` button on editor :) – Pshemo Jul 03 '15 at 16:50

2 Answers2

2

Avoid using \t to get array format. I would probably simply use

System.out.printf("The circle's area          is %.2f %n", circle.getArea() );
System.out.printf("The circle's diameter      is %.2f %n", circle.getDiameter());
System.out.printf("The circle's circumference is %.2f %n", circle.getCircumference());

If there is need to have more dynamic solution you could use

System.out.printf("The circle's %-13s is %.2f%n", "area", circle.getArea());

or if response doesn't need to always start with The circle's

System.out.printf("%-26s is %.2f%n", "The circle's area",  circle.getArea());

DEMO

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • This is exactly the answer I was looking for. All solutions output the correct format. Thank you so much, @Pshemo! – Mark Puchala II Jul 03 '15 at 16:51
  • @OlivierPoulin You are right, output is not precisely the same, but shows direction where correct solution can be found. For me this solution is *close enough* to post it as an answer. – Pshemo Jul 03 '15 at 17:01
  • I.... don't know how to "accept" an answer... I'm new, here. Don't tell nobody, 'kay? EDIT: Found out I just had to click the pretty check-mark. Thanks for notifying me or I would not have come back here to figure it out! – Mark Puchala II Jul 03 '15 at 22:59
  • There is nothing wrong with not knowing what accepting answer is, or how does it work :) Anyway to find many useful informations about Stack Overflow and other Stack Exchange sites visit official [help], or written by community [Meta FAQ](http://meta.stackexchange.com/questions/7931/faq-for-stack-exchange-sites) and [Stack Overflow FAQ](http://meta.stackoverflow.com/questions/251225/faq-index-for-stack-overflow). – Pshemo Jul 03 '15 at 23:07
1

You can try the below example. Do use '-' before the width to ensure left indentation. By default they will be right indented; which may not suit your purpose.

    System.out.printf("%-30s %-5s $%.2f\n","The circle's area ", "is" ,circle.getArea());
    System.out.printf("%-30s %-5s $%.2f\n","The circle's diameter ", "is" ,circle.getDiameter());
    System.out.printf("%-30s %-5s $%.2f\n","The circle's circumference ", "is" ,circle.getCircumference());

Source: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

Olivier Poulin
  • 1,778
  • 8
  • 15
  • I am not sure if `"is"` should be argument. I would probably add it to format instead of `%-5s` since it always exists there. Same about `"The circle's"` part. – Pshemo Jul 03 '15 at 15:53
  • He could, but i thought this format looked more like the original he wanted. Without it, the `is` is awfully close to the numbers. – Olivier Poulin Jul 03 '15 at 15:56
  • But wouldn't `System.out.printf("%-26s is $%.2f\n","The circle's area", circle.getArea());` or even `System.out.printf("The circle's %-13s is %.2f\n", "area", circle.getArea());` be simpler/clearer? Also we should avoid using spaces in arguments. Their places is in format. – Pshemo Jul 03 '15 at 16:04