A problem about java.util.Formatter
hi,all:
I met a trouble in Java, that is the method of "Formatter.format". The "System.out.printf" can output what I want, but why can't "Formatter.format" do that in the under way ? (JDK_1.6) Code:
Formatter formatter = new Formatter(); |
Is this an acceptable solution? I used java.text.DecimalFormat rather than java.util.Formatter:
Code:
import java.text.DecimalFormat; |
Thanks Snark1994 for your solution, it can work too.
But I still don't know why "Formatter.format" can't work in that way? |
Please read the documentation for Formatter.format:
Quote:
Code:
import java.util.Formatter; |
Hi -
Quote:
It doesn't just "return a string" (although, superficially, it might look like it). Rather, it ACTS ON a "string buffer". When you're done (possibly after multiple "format()" operations on your class object), then (and only then) you can call "toString()" on it and get your final, completely formatted string. Two links: Class Formatter The Formatter Class in J2SE 1.5 |
Thanks, I think I should read more document.
|
All times are GMT -5. The time now is 09:02 AM. |