I have a question involving do and for loops while using arrays.
// constants
final int NUMBER_OF_APPS = 50;
final double SENTINEL_VALUE = -1;
// variables
double[] appPurchasePrice = new double[NUMBER_OF_APPS]; // euro
double appPurchasePriceInput;
int a;
// objects
Scanner keyboardInput = new Scanner(System.in);
// solution
// part 1
a = 0;
do
{
System.out.print("Enter app purchase prices (to stop enter -1): ");
appPurchasePriceInput = keyboardInput.nextDouble();
appPurchasePrice[a] = appPurchasePriceInput;
a++;
}
while(appPurchasePriceInput != SENTINEL_VALUE && a <= NUMBER_OF_APPS);
// part 2
System.out.println();
System.out.println("APP PURCHASE PRICE");
// part 3
for (a = 0; a < appPurchasePrice.length; a = a + 1);
{
System.out.println();
System.out.printf("App Purchase Price: %10.2d%n", appPurchasePrice[a]);
}
I keep getting the error that the array index is out of bounds but I'm confused as to why and I want to know how to fix it.
