Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-24-2010, 03:59 AM
|
#1
|
|
LQ Newbie
Registered: Jun 2009
Posts: 5
Rep:
|
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();
int i = 0, j = 0;
float[][] fy = {{1.1f,2.1f,3.1f},
{2.7f,2.4f,6.3f},
{7.1f,8.1f,9f}};
for(i=0;i<3;i++) {
for( j=0;j<3;j++) {
//System.out.printf("%.1f ", fy[i][j]);//It's OK.
System.out.print(formatter.format("%.1f ", fy[i][j]));
}
System.out.println();
}
Last edited by cassati; 12-24-2010 at 04:00 AM.
|
|
|
|
12-24-2010, 07:12 AM
|
#2
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
Is this an acceptable solution? I used java.text.DecimalFormat rather than java.util.Formatter:
Code:
import java.text.DecimalFormat;
public class test {
public test(){
}
public static void main(String[] args){
DecimalFormat formatter = new DecimalFormat("#.#");
int i = 0, j = 0;
float[][] fy = {{1.1f,2.1f,3.1f},
{2.7f,2.4f,6.3f},
{7.1f,8.1f,9f}};
for(i=0;i<3;i++) {
for( j=0;j<3;j++) {
String output = formatter.format(fy[i][j]);
System.out.print(i*3+j);
System.out.print(" ");
System.out.print(fy[i][j]);
System.out.print(" ");
System.out.print(output);
System.out.println();
}
System.out.println();
}
}
}
|
|
|
1 members found this post helpful.
|
12-24-2010, 07:49 AM
|
#3
|
|
LQ Newbie
Registered: Jun 2009
Posts: 5
Original Poster
Rep:
|
Thanks Snark1994 for your solution, it can work too.
But I still don't know why "Formatter.format" can't work in that way?
|
|
|
|
12-24-2010, 11:37 AM
|
#4
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,020
|
Please read the documentation for Formatter.format:
Quote:
Formatter format(String format, Object... args)
Writes a formatted string to this object's destination using the specified format string and arguments.
|
Correct usage of Formatter:
Code:
import java.util.Formatter;
public class Fmt {
static public void main(String[] args) {
Formatter formatter = new Formatter(System.out);
int i = 0, j = 0;
float[][] fy = {{1.1f,2.1f,3.1f},
{2.7f,2.4f,6.3f},
{7.1f,8.1f,9f}};
for(i=0;i<3;i++) {
for( j=0;j<3;j++) {
//System.out.printf("%.1f ", fy[i][j]);//It's OK.
formatter.format("%.1f ", fy[i][j]);
}
System.out.println();
}
}
}
|
|
|
1 members found this post helpful.
|
12-24-2010, 05:56 PM
|
#5
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
Hi -
Quote:
Thanks Snark1994 for your solution, it can work too.
But I still don't know why "Formatter.format" can't work in that way?
|
The short answer, as ntubski pointed out, is because "formatter" is a CLASS, not a "function".
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
|
|
|
1 members found this post helpful.
|
12-25-2010, 05:09 AM
|
#6
|
|
LQ Newbie
Registered: Jun 2009
Posts: 5
Original Poster
Rep:
|
Thanks, I think I should read more document.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:44 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|