LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A problem about java.util.Formatter (https://www.linuxquestions.org/questions/programming-9/a-problem-about-java-util-formatter-852180/)

cassati 12-24-2010 03:59 AM

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();
}


Snark1994 12-24-2010 07:12 AM

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();
                }
        }
}


cassati 12-24-2010 07:49 AM

Thanks Snark1994 for your solution, it can work too.
But I still don't know why "Formatter.format" can't work in that way?

ntubski 12-24-2010 11:37 AM

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();
        }
    }
}


paulsm4 12-24-2010 05:56 PM

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

cassati 12-25-2010 05:09 AM

Thanks, I think I should read more document.


All times are GMT -5. The time now is 09:02 AM.