MissingFormatArgumentException error

落花浮王杯 提交于 2019-12-01 17:29:02

If you are using printf, you need to specify the placeholders as printf parameters along with the format string. In your case you are passing a single string by appending the amount hence the error.

System.out.printf("Total value is: $%.2f\n" + totalValue)

should be replaced by

System.out.printf("Total value is: $%.2f\n", totalValue)

This is the problem:

System.out.printf("Total value is: $%.2f\n" + totalValue);

I think you meant:

System.out.printf("Total value is: $%.2f\n", totalValue);

In other words, specify the value to replace the placeholder with, instead of just using string concatenation to add the value to the send of the format string.

In general though, when you get an exception you don't understand, you should look at the documentation for it. In this case, the docs are reasonably clear:

Unchecked exception thrown when there is a format specifier which does not have a corresponding argument or if an argument index refers to an argument that does not exist.

So there are two things you need to check in your code:

  • Do you have a format specifier without a corresponding argument?
  • Do you have an argument index which refers to an argument that doesn't exist?

You haven't specified any argument indexes in your format string, so it must be the first case - which indeed it is.

System.out.printf("Total value is: $%.2f\n", totalValue); // display product

-> http://www.java2s.com/Code/JavaAPI/java.lang/Systemoutprintf2ffloatf.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!