Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MissingFormatArgumentException error

I have been successful in compiling my inventory program:

// Inventory.java part 1
// this program is to calculate the value of the inventory of the Electronics Department's 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 the item number: 12345
Enter number of items on hand: 8
Enter item Price: 100.50
Department name:Electronics
Item number: 12345
Product name:camera
Quantity: 8
Price per unit100.5
Total value is:
$Exception in thread "main" java.util.MissingFormatArgumentException:
Format specifier '.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Inventory.main(Inventory.java:82)

I seem to come to this format error and cannot see why.

like image 650
Netana Branham Avatar asked Oct 02 '11 08:10

Netana Branham


3 Answers

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)
like image 199
Sanjay T. Sharma Avatar answered Oct 16 '22 06:10

Sanjay T. Sharma


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.

like image 36
Jon Skeet Avatar answered Oct 16 '22 07:10

Jon Skeet


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

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

like image 24
MasterCassim Avatar answered Oct 16 '22 05:10

MasterCassim