# Thread: Need help with sentinels and ArrayLists

1. Member
Join Date
Nov 2012
Posts
1
Rep Power
0

## Need help with sentinels and ArrayLists

I have a project in my programming class. Here are the instructions:

- You must set up an appropriate loop structure with the appropriate sentinel values.
- Your program must store the prices in an array so that they can all be displayed in the final output. Since we are using a sentinel controlled loop here, we will not know how many items the user might be purchasing, so we will make an arbitrary decision here and set the size of the array to hold a maximum of 15 items.
- Your program must also keep a running total of the prices entered. Each price the user enters must be “echoed” back to them as a visual confirmation of what they entered. The program should also present the current total of their purchases up to that point. If they enter an incorrect price, user can press the 0 to remove the last entry, and your code will adjust the running total. The price will also have to be removed from the array. The program should also acknowledge the removal of the last price (see the example output).
- There is no single item in the BigBox Depot store that costs more than \$2,999.99. Use this as a data validation value to make sure the user cannot enter an amount higher than this.
- When user enters -1 to indicate they are done

here is what I have so far:

import java.util.Scanner;
import java.util.ArrayList;

public class Project6
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double price = 0, subTotal = 0;
ArrayList <Double> priceList = new ArrayList<Double>(15);
double [] priceArray = new double [15];

for (int i = 0; price != -1; i++)
{
System.out.print("\nEnter a price for item #" + (i + 1) + ": \$");
price = input.nextDouble();

if (price > 0 && price < 3000)
{
}
else if (price ==0)
{
--i;
System.out.println("Zero entered: Removing last item @ \$" + priceList.get(i));
priceList.remove(i);
subTotal -= priceArray [i];
System.out.print("...your total is now \$" + subTotal + "\n");
continue;
}
else if (price >= 3000)
{
--i;
System.out.println("INVALID PRICE: \$" + price + " please re-enter...");
continue;
}

for (Double priceX: priceList)
{
priceArray [i] = priceX;
}//end for

subTotal += priceArray [i];

System.out.println("That was \$" + priceList.get(i) + ". Your total is \$" + subTotal);
}
System.out.println("\nEND OF PROGRAM");
}
}

For the most part it's working properly. The subtotal is working fine, and it removes items when 0 is entered. The problem I seem to be having is when I enter -1. The program crashes instead of displaying END OF PROGRAM.

I don't even know if I'm doing this properly either ... Don't know how to do math with ArrayList so I made an array that hold's the ArayList's "values".

2. Member
Join Date
Sep 2012
Posts
11
Rep Power
0

## Re: Need help with sentinels and ArrayLists

Hello ,

use code tags , regarding that your programs crashes when you type -1 , read up on try catch statements.

Best regards
Another beginner.

3. Moderator
Join Date
Apr 2009
Posts
13,312
Rep Power
23

## Re: Need help with sentinels and ArrayLists

What error are you getting?
Please include the complete stack trace.
No point using a try/catch if the error shouldn't be happening anyway.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•