Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

$Exception in thread \"main\" java.util.IllegalFormatConversionException: f != j

ID: 3569311 • Letter: #

Question

$Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String

   at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)

   at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)

   at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)

   at java.util.Formatter.format(Formatter.java:2520)

   at java.io.PrintStream.format(PrintStream.java:970)

   at java.io.PrintStream.printf(PrintStream.java:871)

   at WebStore2.main(WebStore2.java:105)   

Can someone tell me why?

import java.util.Scanner;

public class WebStore2
{
   public static void main(String[] args)
   {
       /**array declarations and initializations**/
       //TO-DO: initialize the itemNames array with the item names
       //(e.g. "eReader", "Java textbook", ...)
       String[] itemNames = {"eReader", "Java textbook", "Flash drive", "Netbook", "Python textbook", "Laptop PC", "Headphones", "Tablet PC", "Wireless keyboard", "Laptop briefcase"};
       //TO-DO: initialize the price array (e.g. 180.0, 85.0, ...)
       double[] priceArray = {200.00, 85.00, 15.00, 250.00, 90.00, 725.00, 55.00, 320.00, 60.00, 40.00};
       int[] shoppingCart = new int[10];
       //TO-DO: initialize the accountNumbers array
       //(e.g. "5658945", "4528125", ...)
       int[] accountNumbers = {5658945, 4528125, 7805122, 8777521, 8751277, 1202850, 1005331, 6542231, 3812085, 7576631, 7981200, 4281002};

      
       /**other variable declarations and initializations**/
       Scanner keyboard = new Scanner(System.in);
       int menuChoice = 0;
       String userName = "";
       int itemNumber;   //used to store item selection during "item add" operation
       int quantity;   //used to store quantity during "item add" operation
       boolean cartIsEmpty = true;
       String userAccountNumber = "";   //used in the checkout operation
       boolean accountNumberIsValid = false;   //used in the checkout operation
      
      
       //print welcome message
       System.out.println("Welcome to the Java Web Store!");
      
       //start main program loop
       do
       {
           //print the menu options
           //TO-DO: write print statements to print the menu
           System.out.println("");
           System.out.println("Menu options: ");
           System.out.println("1. View item selection");
           System.out.println("2. View shopping cart");
           System.out.println("3. Add item to cart");
           System.out.println("4. Remove item from cart");
           System.out.println("5. Checkout");
           System.out.println("6. Exit");
          
           //prompt the user to enter a menu option
           System.out.print("Please enter your selection: ");
           menuChoice = keyboard.nextInt();
          
           //use a switch statement to act based on the value of
           //menuChoice variable
           switch(menuChoice)
           {
              
               case 1:   //view item selection
                   //TO-DO: write print statements to print the
                   //item selections
                   System.out.println("Item selection");
                   System.out.println("1. eReader $200.00");
                   System.out.println("2. Java textbook $85.00");
                   System.out.println("3. Flash drive $15.00");
                   System.out.println("4. Netbook $250.00");
                   System.out.println("5. Python textbook $90.00");
                   System.out.println("6. Laptop PC $725.00");
                   System.out.println("7. Headphones $55.00");
                   System.out.println("8. Tablet PC $320.00");
                   System.out.println("9. Wireless keyboard $60.00");
                   System.out.println("10. Laptop briefcase $40.00");
              
                   break;
              
               case 2:   //view shopping cart
                   //first determine if the cart is empty
                   //how to do this: assume cart is empty, and if we find
                   //an entry > 0, infer that cart is non-empty
                   //use the flag variable cartIsEmpty to keep track of this
                   cartIsEmpty = true;
                   for (int i = 0; i < shoppingCart.length; i++)
                   {
                       if (shoppingCart[i] > 0)
                           cartIsEmpty = false;
                   }
                  
                   //print a message if cart is empty
                   if (cartIsEmpty)
                       System.out.println("Your shopping cart is currently empty.");
                   else
                   {
                       //print the title and table heading
                       System.out.println(" Shopping Cart Contents:");
                       System.out.printf("%-25s%-25s%10s", "Item", "Quantity", "Cost ");
                       System.out.println("-------------------------------------------");
                      
                       //loop through the shoppingCart array and print a table row for
                       //each item that has an entry > 0
                      
                       for (int i = 0; i < shoppingCart.length; i++)
                       {
                           if (shoppingCart[i] > 0)
                           {
                               //TO-DO: complete the print statement
                               System.out.printf("%-25s%-25d$%10.2f", i++, shoppingCart[i],itemNames[i], priceArray[i] );
                           }
                       }
                   }
      
                   break;
                  
                   case 3:   //add item to cart
                       //prompt user for item number and quantity
                       //TO-DO: prompt user for item number; store input in the
                       //variable itemNumber
                      
                      
                       //TO-DO: prompt user for quantity; store input in the
                       //variable quantity
                       Scanner keyboarditem = new Scanner(System.in);
                       Scanner keyboardquant = new Scanner(System.in);
                       System.out.print("Enter an item number: ");
                       itemNumber = keyboarditem.nextInt();
                       System.out.print("Enter the quantity: ");
                       quantity = keyboardquant.nextInt();
                      
                       //TO-DO: update the shopping cart array
                       shoppingCart[itemNumber-1] += quantity;
                      
                      
                       //TO-DO: print a confirmation message to the user
                       //e.g. "Added 4 Headphones to your cart."
                       System.out.println("Added " + shoppingCart[quantity] + " " + itemNames[menuChoice] + " to your cart.");
                      
                      
                       break;
                  
                   case 4:   //remove item from cart
                       //prompt user for item number and quantity
                       Scanner keyboarditem2 = new Scanner(System.in);
                       Scanner keyboardquant2 = new Scanner(System.in);
                       System.out.print("Enter an item number: ");
                       itemNumber = keyboarditem2.nextInt();
                       System.out.print("Enter the quantity: ");
                       quantity = keyboardquant2.nextInt();
                      
                      
                       //perform remove operation if possible
                       //the operation is not possible if, for the item, we have:
                       //(quantity to remove) > (quantity in cart)
                      
                       //TO-DO: fill in the proper index for the shoppingCart array
                       if (quantity > shoppingCart[quantity])
                       {
                           //TO-DO: fill in the proper index for the shoppingCart array and the itemNames array
                           System.out.print("You have " + shoppingCart[quantity] + " " + itemNames[menuChoice] + " in your cart. " +
                                           "Nothing was removed. ");
                       }
                       else
                       {
                           //perform remove operation and print confirmation message to user
                           //TO-DO: remove the quantity from the proper element of the shoppingCart array
                           shoppingCart[itemNumber-1] -= quantity;
                          
                          
                           //TO-DO: print a confirmation message to the user
                           //e.g. "Removed 1 Headphones from your cart."
                           System.out.println("Removed" + quantity + " " + itemNames + " from your cart.");
                          
                          
                       }
                      
                       break;
                      
                   case 5:   //checkout
                       //prompt user to enter an account number; store input in variable userAccountNumber
                       System.out.print("Please enter your account number to complete your order: ");
                       userAccountNumber = keyboard.nextLine();
                      
                       //check if the account number is valid
                       //how to do this: assume account number is invalid; then iterate
                       //through the accountNumbers array, each time comparing userAccountNumber to the
                       //current element of accountNumbers to see if there is a match
                       //use the flag variable accountNumberIsValid to keep track of this
                       accountNumberIsValid = false;
                      
                       //we will loop until the user enters a valid account number
                       do
                       {
                          
                      
                           for (int i = 0; i < accountNumbers.length; i++)
                           {
                               if (accountNumbers[i] == (Integer.parseInt(userAccountNumber)))
                               {
                                   accountNumberIsValid = true;
                               }
                           }
                          
                           //TO-DO: complete the if-else statement
                           if (accountNumberIsValid)
                           {
                               System.out.println("Your order is complete! Thank you!");
                          
                           }
                           else
                           {
                               Scanner keyboardaccount = new Scanner(System.in);
                               System.out.print("I

Explanation / Answer

Because of the wrong formatting in case 2, 2nd for loop:

for (int i = 0; i < shoppingCart.length; i++)
{
if (shoppingCart[i] > 0)
{
//TO-DO: complete the print statement
System.out.printf("%-25s%-25d$%10.2f", shoppingCart[i],itemNames[i], priceArray[i] );
}
}

it should be

  System.out.printf("%-25s%-25d$%10.2f", itemNames[i],shoppingCart[i],priceArray[i] );