Java 尝试格式化字符串时出现 IllegalFormatPrecisionException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2224719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IllegalFormatPrecisionException while trying to format string
提问by relyt
I'm trying to write a program which prompts the user to enter two 3×3 matrices and displays their product.
我正在尝试编写一个程序,提示用户输入两个 3×3 矩阵并显示他们的产品。
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();
}
}
}
采纳答案by Michael Myers
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).
您正在使用说明%d
符,它需要一个整数参数——但是您给了它一个String
(因为在连接它时c[i][j]+" "
转换c[i][j]
为String
)。
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.
此外,说明%d
符根本不使用小数点。由于整数类型可以隐式转换为浮点类型,因此%f
说明符就是您要查找的内容。
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.
最后,格式说明符中小数点后的数字告诉它要到多少小数位。你说你只想要一位小数,所以让我们把它设为 1。
So what we end up with is this:
所以我们最终得到的是这样的:
System.out.printf("%.1f ", c[i][j]);
See the Formatter
Javadocsfor 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.)
有关所有可能的格式说明符的(有点令人难以置信的)描述,请参阅Formatter
Javadoc。(如果您无法理解那里的所有内容,请不要太担心;无论如何,您永远不需要其中的大部分内容。)
回答by Drejc
Your error is probably here:
你的错误可能在这里:
System.out.printf("%.2d", c[i][j]+" ");
Check the documentation of how values are formatted when printed out.
检查打印时如何格式化值的文档。
回答by Mark Byers
You can't format integers by using a decimal pointin the conversion. Since c[i][j]
is a double, you can use a floating pointconversion:
您不能通过在转换中使用小数点来格式化整数。由于c[i][j]
是双精度,您可以使用浮点转换:
System.out.printf("%.2f ", c[i][j]);
Instead of:
代替:
System.out.printf("%.2d", c[i][j]+" ");
See the help pagefor the formatter syntax for more information.
有关更多信息,请参阅格式化程序语法的帮助页面。
回答by Uri
Are you sure you meant to say "%.2d" in your printf rather than "%.2f"? You usually only use d for integer values, you have doubles in your matrix.
您确定要在 printf 中说“%.2d”而不是“%.2f”吗?您通常只将 d 用于整数值,您的矩阵中有双精度值。