LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 12-24-2010, 03:59 AM   #1
cassati
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Rep: Reputation: 0
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.
 
Old 12-24-2010, 07:12 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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.
Old 12-24-2010, 07:49 AM   #3
cassati
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks Snark1994 for your solution, it can work too.
But I still don't know why "Formatter.format" can't work in that way?
 
Old 12-24-2010, 11:37 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,445

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
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.
Old 12-24-2010, 05:56 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
Old 12-25-2010, 05:09 AM   #6
cassati
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks, I think I should read more document.
 
  


Reply

Tags
java


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The import java.util is never used farhan.azam Linux - Newbie 4 06-06-2009 07:52 AM
Where is java.util.Scanner? pda_h4x0r Programming 8 11-12-2007 03:53 PM
java: java.util.Hashmap doesn't work for a custom object kpachopoulos Programming 2 04-15-2007 03:25 PM
java.util.*; in Linux? vmlinuz.gz Programming 3 04-05-2006 02:26 PM
Java can't find package, but PATH set correctly (j2se1.4.0, java.util.regex package) Ethan Programming 5 02-06-2004 09:55 PM


All times are GMT -5. The time now is 09:55 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration