-1

I've written this program but am running into a logical error upon compilation. Getting index.arrayOutOfBounds?

My input would be 1, 2, 6, 10 for the selection of products and the coinciding output should be

Total items ordered: 3
Price of items ordered: $747.00
Sales Tax: $48.55
Total amount due: $795.55

Is there an error within my for loop conditions or calculations, or my array that is leading to this output?

import java.util.Scanner;
public class GrapefruitOrderingArray {

//Declare Constants 
public static final int SIZE = 100;
public static final int[] itemPrices = {49,299,329,399,199,1299,1199,999,599};  

public static void main(String[] args) {
// Declare Variables
    Scanner input = new Scanner (System.in);                                    
    String CustomerName;                                                       
    int[] naNumber = new int [SIZE];                                            
    int nProducts = 0;                                                        
    double nTotal = 0;                                                          
    double dFinalPrice = 0.0;                                                   
    int nCount = 0;                                                             

    //Declare Constants 
    final int SENTINEL = 10;   
    final double SALES_TAX = 0.065;

    //Prompt user to enter name
    System.out.println("Please enter your name: ");

    //Enter user name
    CustomerName = input.nextLine();

    System.out.println("");

    //Begin Product Listing Declarations with respect to array above
    System.out.println("GRAPEFRUIT PRODUCT:");
    System.out.println("1. gPod shuffle $" + itemPrices[0]);
    System.out.println("2. gPod Touch   $" + itemPrices[1]);
    System.out.println("3. gPad Mini    $" + itemPrices[2]);
    System.out.println("4. gPad 2       $" + itemPrices[3]);
    System.out.println("5. gPhone       $" + itemPrices[4]);
    System.out.println("6. gMac         $" + itemPrices[5]);
    System.out.println("7. MacNovel Pro $" + itemPrices[6]);
    System.out.println("8. MacNovel Air $" + itemPrices[7]);
    System.out.println("9. MiniMac      $" + itemPrices[8]);
    System.out.println("10. Complete my order");

    //Keep reading until the input is terminated by sentinel
    System.out.println("\nPlease select an item from the menu above: ");

    //Read number entered by the user
    naNumber[nCount] = input.nextInt();

    //Begin while-loop statement
    while (naNumber[nCount] != SENTINEL) {

    System.out.println("\nPlease select another item from the menu above: ");

    nCount++;

    //Read number entered by the user
    naNumber[nCount] = input.nextInt();
 }  

    System.out.println("Thank you for ordering with Grapefruit Company, " + CustomerName);
        //Call final price calculation
        dFinalPrice = calculateTotalPrice(naNumber,itemPrices,nTotal);

            //Print blank line to screen
            System.out.println("");

            //Total amount of product ordered
            System.out.println("Total items ordered: " + nCount );

            //Total price of items ordered
            System.out.println("Price of items ordered: $" + dFinalPrice );

            //Sales tax associated with the purchase
            System.out.println("Sales tax: $" + SALES_TAX * dFinalPrice );

            //Total amount due by the customer to Grapefruit Co. 
            System.out.println("Total amount due: $" + (SALES_TAX * dFinalPrice + dFinalPrice ));
    } //End main method

 private static double calculateTotalPrice(int[] naNumber, int[] itemPrices) {

  double total = 0;

  //Calculate entered items
  for(int i = 0; i < naNumber.length; i++){
   if(naNumber[i] != 0) {
   total += itemPrices[naNumber[i] - 1];
  }
}

  return total;
  }
} //end class calculateTotalPriceOfItemsOrdered
  • 2
    Consider using a debugger. – Sotirios Delimanolis Oct 22 '13 at 20:57
  • What is the stack trace? Any decent IDE will display the line for the exception. It is a waste of time for us to debug what your IDE can do instantly. – Zong Oct 22 '13 at 21:01
  • Inside calculateTotalPrice() function the for loop continues looping till i is less than naNumber.length(naNumber.length = 100). But itemPrice array contains only 9 values. So after that it causes ArrayIndexOutOfBound exception. – Md Ashaduzzaman Oct 22 '13 at 21:10

2 Answers2

0

You array itemPrices is having 9 elements. So when you input 10 it would make the look execute like this :

total += itemPrices[10 - 1];

Your array doesn't hold any element at 10th position i.e. itemPrices[9]. Hence ArrayIndexOutOfBoundException

Crickcoder
  • 2,135
  • 4
  • 22
  • 36
0

you problem because after finish of your loop you have naNumber[nCount] == 10; and then in calculateTotalPrice you're trying to access total += itemPrices[9]; which is out of bounds

quick solution: after your while add naNumber[nCount] = 0;

but you have to consider what you application will do if user entered more than 100 products?

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57