MissingFormatArgumentException Error

I managed to compile my inventory program:

// Inventory.java part 1 // this program is to calculate the value of the inventory of the Electronics Department cameras import java.util.*; import javax.swing.*; import java.awt.event.*; import java.io.*; public class Inventory { public static void main(String[] args) { // create Scanner to obtain input from the command window Scanner input = new Scanner (System.in); String name; int itemNumber; // first number to multiply int itemStock; // second number to multiply double itemPrice; // double totalValue; // product of number1 and number2 while(true){ // infinite loop // make new Camera object System.out.print("Enter Department name: "); //prompt String itemDept = input.nextLine(); // read name from user if(itemDept.equals("stop")) // exit the loop break; { System.out.print("Enter item name: "); // prompt name = input.nextLine(); // read first number from user input.nextLine(); } System.out.print("Enter the item number: "); // prompt itemNumber = input.nextInt(); // read first number from user input.nextLine(); while( itemNumber <= -1){ System.out.print("Enter valid number:"); // prompt itemNumber = input.nextInt(); // read first number from user input.nextLine(); } /* while statement with the condition that negative numbers are entered user is prompted to enter a positive number */ System.out.print("Enter number of items on hand: "); // prompt itemStock = input.nextInt(); // read first number from user input.nextLine(); while( itemStock <= -1){ System.out.print("Enter positive number of items on hand:"); // prompt itemStock = input.nextInt(); // read first number from user input.nextLine(); } /* while statement with the condition that negative numbers are entered user is prompted to enter a positive number */ System.out.print("Enter item Price: "); // prompt itemPrice = input.nextDouble(); // read second number from user input.nextLine(); while( itemPrice <= -1){ System.out.print("Enter positive number for item price:"); // prompt itemPrice = input.nextDouble(); // read first number from user input.nextLine(); } /* while statement with the condition that negative numbers are entered user is prompted to enter a positive number */ Cam camera = new Cam(name, itemNumber, itemStock, itemPrice); totalValue = itemStock * itemPrice; // multiply numbers System.out.println("Department name:" + itemDept); // display Department name System.out.println("Item number: " + camera.getItemNumber()); //display Item number System.out.println("Product name:" + camera.getName()); // display the item System.out.println("Quantity: " + camera.getItemStock()); System.out.println("Price per unit" + camera.getItemPrice()); System.out.printf("Total value is: $%.2f\n" + totalValue); // display product } // end while method } // end method main }/* end class Inventory */ class Cam{ private String name; private int itemNumber; private int itemStock; private double itemPrice; private String deptName; public Cam(String name, int itemNumber, int itemStock, double itemPrice) { this.name = name; this.itemNumber = itemNumber; this.itemStock = itemStock; this.itemPrice = itemPrice; } public String getName(){ return name; } public int getItemNumber(){ return itemNumber; } public int getItemStock(){ return itemStock; } public double getItemPrice(){ return itemPrice; } } 

C: \ Java> java Inventory
Enter department name: Electronics
Enter item name: camera
Enter item number: 12345
Enter the number of items on hand: 8
Enter product Price: 100.50
Department Name: Electronics
Item Number: 12345
Product name: camera
Quantity: 8
Unit Price100.5
Total value:
$ Exception in thread "main" java.util.MissingFormatArgumentException:
Format Specifier ..2f '
in java.util.Formatter.format (Unknown source)
in java.io.PrintStream.format (Unknown source)
in java.io.PrintStream.printf (Unknown source)
in Inventory.main (Inventory.java:82)

It seems that I came to an error of this format and do not understand why.

+6
source share
3 answers

If you use printf , you need to specify placeholders as printf parameters along with the format string. In your case, you pass one line, adding 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) 
+19
source

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.

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

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

+2
source

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


All Articles