0

I have some issues with printing my code. I want my result to be aligned neatly but it isn't like that. It would be very grateful if someone could help me with the issue.

my code is as follows :

public static void main(String[] args) {
    for(int j=1 ; j<=31;j++) {
        System.out.printf("%10d",j);
        if(j%7==0) {
            System.out.println();
        }

    }
}

the result I get to see on my screen as belows

enter image description here

6
  • 2
    try: System.out.printf("%-10s",j); Commented Jun 2, 2018 at 7:50
  • @user202729 i posted it now as an anwer Commented Jun 2, 2018 at 8:03
  • For OP: It would be better for you if you explain more what do you expect.What is 'neat'? Commented Jun 2, 2018 at 8:04
  • @user202729 neatly means accurately Commented Jun 2, 2018 at 8:06
  • @Rcordoval (1) I'm telling OP not you (2) That's not precise enough. Both answers are assuming OP want the output left-aligned, but what if OP want it to be right/center/sth-else aligned? Unclear. Commented Jun 2, 2018 at 8:08

2 Answers 2

1

Your code is correct. But you should use MONOSPACED (fixed width) font like Courier or Lucida Console in your terminal / console:

  • you use not monospaced font in your OS terminal
  • or you use not monospaced font in the console of your IDE
  • or you forward your output to file that you view in some editor again with not monospaced font
  • or something similar with not monospaced font
Sign up to request clarification or add additional context in comments.

3 Comments

Using a fixed width font is the correct advice. However, you are mixing things up in your answer. True type refers to a font technology (font format) and can be used for fixed width and variable width fonts alike. MONOTYPE is a company providing fonts and tools.
Thank you so much I don't really understand what monotype font or true type is but changing a font solved my problem.
@Henry : Yes, I meant monospaced, not monotype :) :) But you are right about TTF, I vote your comment up :)
1

According to Formatting Numeric Print Output

Format specifiers begin with a percent sign (%) and end with a converter. The converter is a character indicating the type of argument to be formatted. In between the percent sign (%) and the converter you can have optional flags and specifiers

In this case:

  1. % is format syntax mandatory prefix.
  2. - is the flag for Left-justified.
  3. 10 means ten characters in width, with leading zeroes as necessary.
  4. d, A decimal integer.

So your main method should be:

public class Tst {
    public static void main (String[] args)
    {
        for(int j=1 ; j<=31;j++) {
            System.out.printf("%-10d",j);
            if(j%7==0) {
                System.out.println();
            }

        }
    }
}

Giving the resulting output:

enter image description here

1 Comment

thank for your help but the result looks the same as before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.