1
public class Format
{
    public static void main(String args[])
    {
        System.out.printf("%30s|%30s","Organization","Number of users");
        System.out.printf("%30s|%30s","Arcot","100");
    }
}

It prints:

          Organization|               Number of users                          Arcot|                           100

Why is the 2nd row out of alignment? The word "Arcot" is not given enough padding, although the word "100" is. I'm sorry, this text window applies its own formatting, it is not showing what I have pasted as the output. You may need to run the code to see the output obtained.

1
  • format the "output" as code, than you can easily show the effect ;) Commented Feb 16, 2011 at 7:21

3 Answers 3

1

Try these.

System.out.println(String.format("%30s|%30s","Organization","Number of users"));
    System.out.println(String.format("%30s|%30s","Arcot","100"));
Sign up to request clarification or add additional context in comments.

Comments

0
System.out.printf("%30s|%30s\n","Organization","Number of users");
System.out.printf("%30s|%30s\n","Arcot","100");

You have to insert \n issue a newline character end of first parameter of printf.

More info about using escape character.

Comments

0

This works as expected:

System.out.printf("%30s|%30s%n","Organization","Number of users");
System.out.printf("%30s|%30s%n","Arcot","100");

results in

              Organization|               Number of users
                     Arcot|                           100

on my machine. You didn't add line feeds. %n is the preferred notation in format Strings.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.