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();
}
}
}
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.
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.
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.
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With