Hello,

Can anyone show me how to set width between character in java. I'm looking for something like this.

Items               NumOfItems                Price
A                      2                       $10
B                      1                        $5

When I use the for loop to make something like the table above it shows something like this.

Items               NumOf Items               Price
A                2                   $10
B                         1                         $5

They are scattered the way I don't wait them to be.

How do I fix that.? Here is my simple code.

for(int i = 0; i < ITEM; i++){

                        int[] sold = {10, 10, 10, 10, 10, 10, 10, 10, 10};

                                System.out.println(i+1 +"."+ items[i] +"\t\t\t\t"+ +count[i]+"("+(sold[i] - count[i])+")" + "\t\t\t" + 
                                "$"+(df.format((sold[i]-count[i])*price[i])));
                    }

You could use System.out.format to achieve that result. It requires a format string and variables.

A format string is built up as follows.

String contentFormat = "%-15s %-8d %-15s %n";

In the %-15s for instance: a % indicates a new "column". The - means that writing starts from the left, the 15 means it will have a total of 15 characters in width which it will try to fill with your data, the s means that the input type will be a string. It's the same for %-8d with the difference being the d which stands for digit. The %n is a newline). The spaces (or any other characters) you put in will be printed normally. So in this case you have a "column" for strings that is 15 characters in width, then a whitespace, then an 8 character wide column for digits, another whitespace, then a 15 width string again, followed by a space, then a newline.

For the actual print command you would then use something like:

 System.out.format(contentFormat, "A", 1, "$9,00");

First part is the format string variable, the other parameters are your values (they must be in the same type and order as specified in the format string, also the total amount of variables must be the same as in the format string).

Since your title has different types of variables you could write two different formats, one for the title and one for the content. Something like:

String contentFormat = "%-15s %-8d %-15s %n";
String titleFormat = "%-15s %-8s %-15s %n";

System.out.format(titleFormat, "Items", "Amount", "Price");
System.out.format(contentFormat, "A", 1, "$9.00");
System.out.format(contentFormat, "B", 2, "$9.50");
System.out.format(contentFormat, "C", 5, "$80");
System.out.format(contentFormat, "D", 3, "$9");

Which would give you the result:

Items           Amount   Price           
A               1        $9.00           
B               2        $9.50           
C               5        $80             
D               3        $9              

For more information also see this discussion about ASCII tables.

commented: Exactly +0

you went straight to the point. thanx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.