Question

  1. Create a java file in NetBeans, name it Catalog.java.
  2. Create one String array to store 3 products for a catalogue
  3. Create the appropriate variables to store the product name, product code and product price.
  4. At the start of the program display the catalogue for the user by looping through the array and outputting it to the screen with a number listing for each product, as shown above
  5. Create an infinite loop to enter orders; to stop the loop the user should enter 0.
  6. Keep a running total (accumulator) of all products amount total and sub-total (multiple accumulators)
  7. Write a method to calculate the taxes and return a grand total
  8. Write another method to print out the order as listed above

its supposed to be entered like this

I would like to know how to have the user input like this and be stored in an array.

Enter Order Number (0 to stop): M3487 
Enter Quantity: 2 
Enter Order Number (0 to stop): W3876 
Enter Quantity: 3
Enter Order Number (0 to stop): R9983 
Enter Quantity: 3 
Enter Order Number (0 to stop): 0 

when i enter the code "M3487" its not going to the quantity, its ending the program.

This is the code I have so far. I'm a beginner, so please bear with me.

package catalog;

import java.util.*;

public class Catalog {

static String products[] = new String[3];
static int answer;

 public static void main(String[] args) {

         System.out.println("------------------");
         System.out.println("Shopping Catalog");
         System.out.println("------------------");

          String[] pCode = new String[3];
          float pPrice[] = new float[3];
          int orderNum = 0;
          int quantity=0;

         Scanner s = new Scanner(System.in);
         System.out.println("------------------------------------------");
            System.out.println("condensed milk [M3487], $9.50 per can.");
                System.out.println("");
            System.out.println("Distilled Water [W3876], $3.00 a bottle.");
                System.out.println("");
            System.out.println("Pack Rice [R9983], $12.75 for 5lbs.");
         System.out.println("------------------------------------------");




            do{

                     System.out.println("Please enter order number (0 to stop)");
                                pCode[orderNum] = s.nextLine();
                                    orderNum++;
                                        if(pCode[orderNum] == ("M3487")){
                                            System.out.println("condensed milk $9.50");
                                                System.out.println("Enter Quantity");
                                                    quantity = s.nextInt();
                                        }//close if statement
                 if(answer == 0){
                     break;
                 }//close if
            }while(true);//close while loop

    }//close main method

}//close class
Was it helpful?

Solution

System.out.println("Please enter order number (0 to stop)");
pCode[orderNum] = s.nextLine();
orderNum++;             //This is the problem
if(pCode[orderNum] == ("M3487"))//It will not work because you have change the index

Remove

orderNum++;

because You have inserted string at pCode[0] zero index and searching at pCode[1].

Change you code to:

System.out.println("Please enter order number (0 to stop)");
pCode[orderNum] = s.nextLine();                                   
if(pCode[orderNum].equals("M3487")){
    System.out.println("condensed milk $9.50");
    System.out.println("Enter Quantity");
    quantity = s.nextInt();
}
orderNum++;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top