IllegalFormatPrecisionException when trying to format a string

I am trying to write a program that will prompt the user to enter two 3 Γ— 3 matrices and display their product.

For example, a user may 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 I tried, but I keep getting this error. Any help to point me in the right direction would be greatly appreciated. I am trying to do this with an accuracy of ten characters:

  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(); } } } 
+4
source share
4 answers

You use the %d specifier, which requires an integer argument, but you give it a String (because c[i][j]+" " converts c[i][j] to String when it is concatenated).

In addition, 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 are looking for.

And finally, the number after the decimal point in the format specifier is what tells him how many decimal places to move. You say you want only one decimal place, so let it do 1.

So, the result is the following:

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

See Formatter Javadocs for a (a bit overwhelming) description of all possible format specifiers. (Don’t worry too much if you can’t understand everything and you won’t need most anymore.)

+15
source

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

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

Instead:

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

See the man page for formatting syntax for more information.

+5
source

Perhaps your mistake:

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

Check the documentation for how the values ​​are formatted when printed.

0
source

Are you sure you want to say "% .2d" in your printf, not "% .2f"? Usually you use only d for integer values, you have doubles in your matrix.

0
source

Source: https://habr.com/ru/post/1300676/


All Articles