String formatting alignment
I am trying format marry string so they align in the same way e.g.
This is how I would like them to be formatted:
Code :
Cricket $100
Football $100
Basketball $1
The total Cost is $201
This is the method I have tried though I get mix results when different inputs are given, sometimes it works sometimes it doesn't.
This is what it mostly ends up like:
Code :
Cricket $100
Football $100
Basketball $1
The total Cost is $201
I have tried experimenting by having different right-align in code for total cost though nearly always get wayward results, though it has worked a couple of times on different values of variable length where right-align and left-align were different.
This is the code I have tried:
Code :
System.out.printf("%-15s %15s %n", " Cricket", "$100");
System.out.printf("%-15s %15s %n", " Football", "$100");
System.out.printf("%-15s %15s %n", " Basketball", "$1");
System.out.printf("%-15s %15s %n", "The total Cost is", "$201");
I am not bothered about how much spacing there is in between, I just want to align the $ signs as I have managed to align the sports. Where am I going wrong?
Re: String formatting alignment
Quote:
Originally Posted by
keepStriving
This is how I would like them to be formatted:
Code :
Cricket $100
Football $100
Basketball $1
The total Cost is $201
This is a solution
Code :
System.out.printf("%-24s %s %n", " Cricket", "$100");
System.out.printf("%-24s %s %n", " Football", "$100");
System.out.printf("%-24s %s %n", " Basketball", "$1");
System.out.printf("%-24s %s %n", "The total Cost is", "$201");
Note the %-24s (just to be sure that "The total Cost is" does not exceed) and that the second specification is simply %s (so left aligned as you want).
Re: String formatting alignment
I would use StringBuilder's insert(int offset, String str) to force a column. I don't use printf much so I can't say what is going on.
Code Java:
public void printToColumn(String item, String cost) {
int itemColumn = 1;
int priceColumn = 15;
StringBuilder b = new StringBuilder();
b.append(" ");
b.insert(itemColumn, item);
b.insert(priceColumn, cost);
System.out.println(b.toString());
}
Re: String formatting alignment
Thanks guys, I was wondering what made you choose -24% of all numbers you could use?
I got printf to work for terminal window which is what I wanted though not for JTextArea which still prints it wayward, I'll try String Builder for JTextArea.
Re: String formatting alignment
You didn't mention a JTextArea. Font spacing and kerning are important in this situation so use a Monospace font like Courier. See SWT - OS agnostic way to get monospaced font for a platform independent solution.
Re: String formatting alignment
I am passing the correct alignment to JTextArea as I've done a print verification though JTextArea is still not aligning correctly, is this a trait of JText Area?
Re: String formatting alignment
Quote:
Originally Posted by
keepStriving
not for JTextArea which still prints it wayward
Text alignment in a text component that uses a variable-width font is not easy (and StringBuffer/StringBuilder don't help much since the problem is not about character units but pixel units, depending on the font and single characters).
Something is possible using tabs ('\t') and see also the documentation of setTabSize of JTextArea. However, if you can use a fixed-width font in JTextArea it's really better.
Re: String formatting alignment
This code sorted the problem.
Code :
new Font(Font.MONOSPACED, Font.PLAIN, 12);