Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalFormatPrecisionException while trying to format string

Tags:

java

I'm trying to write a program which prompts the user to enter two 3×3 matrices and displays their product.

For example, a user can enter:

Matrix A: 2 4 6 8 10 12 14 16 18
Matrix B: 1 2 3 4 5.6 6.6 7.4 8.1 9

Below is what I have tried, but I keep getting this error. Any help to point me in the right direction would be appreciated. I'm trying to get it to one decimal place:

Exception in thread "main" java.util.IllegalFormatPrecisionException: 2
    at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2892)
    at java.util.Formatter$FormatSpecifier.(Formatter.java:2643)
    at java.util.Formatter.parse(Formatter.java:2480)
    at java.util.Formatter.format(Formatter.java:2414)
    at java.io.PrintStream.format(PrintStream.java:920)
    at java.io.PrintStream.printf(PrintStream.java:821)
    at Exercise6_25.main(Exercise6_25.java:55)

import java.util.Scanner;

public class matrixCalc
{
    public static void main(String args[])
    {
        Scanner s= new Scanner(System.in);

        int i,j,k;
        int n=3;

        double a[][]= new double[n][n];
        double b[][]= new double[n][n];
        double c[][]= new double[n][n];

        System.out.println("enter the array elements of a:");

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                a[i][j]=s.nextDouble();
            }
            System.out.print(" ");
        }

        System.out.println(" "); 
        System.out.println("enter the array elements of b:");

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                b[i][j]=s.nextDouble();
            }
            System.out.print(" ");
        }

        System.out.println(" ");
        System.out.println("the result matrix is:");

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                for(k=0;k<n;k++)
                {
                    c[i][j]+=a[i][k]*b[k][j];
                }
            }
        }

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.printf("%.2d", c[i][j]+" ");
            }
            System.out.println();
        }
    }
}
like image 637
relyt Avatar asked Feb 08 '10 20:02

relyt


People also ask

What is format method in string?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.

Does format return a string?

The Java String. format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String.

Why do we use format string?

The main reason is that String. format() can be more easily localised with text loaded from resource files whereas concatenation can't be localised without producing a new executable with different code for each language.


2 Answers

You are using the %d specifier, which requires an integer argument -- but you are giving it a String (because c[i][j]+" " converts c[i][j] to String when concatenating it).

Also, the %d specifier does not use a decimal point at all. Since integral types can be implicitly converted to floating-point types, the %f specifier is what you're looking for.

And finally, the number after the decimal point in the format specifier is what tells it how many decimal places to go to. You say you only want one decimal place, so let's make that a 1.

So what we end up with is this:

System.out.printf("%.1f ", c[i][j]); 

See the Formatter Javadocs for a (somewhat mind-boggling) description of the all possible format specifiers. (Don't worry too much if you can't understand everything there; you'll never need most of it anyway.)

like image 64
Michael Myers Avatar answered Sep 30 '22 05:09

Michael Myers


You can't format integers by using a decimal point in the conversion. Since c[i][j] is a double, you can use a floating point conversion:

System.out.printf("%.2f ", c[i][j]);

Instead of:

System.out.printf("%.2d", c[i][j]+" ");

See the help page for the formatter syntax for more information.

like image 32
Mark Byers Avatar answered Sep 30 '22 04:09

Mark Byers