This is the problem:
System.out.printf("Total value is: $%.2f\n" + totalValue);
I think you mean:
System.out.printf("Total value is: $%.2f\n", totalValue);
In other words, specify a value to replace the placeholder instead of just using string concatenation to add the value to the format string sending.
In the general case, when you get an exception that you do not understand, you should look at the documentation for it. In this case, the documents are clear enough :
An exception is thrown if there is a format specifier that does not have a corresponding argument, or if the argument index refers to an argument that does not exist.
So you need to check two codes:
- Do you have a format specifier with no corresponding argument?
- Do you have an argument index that refers to an argument that does not exist?
You did not specify any argument indices in your format string, so this should be the first case - and it really is.
source share