Results 1 to 3 of 3
  1. #1
    Ozai is offline Member
    Join Date
    Nov 2012
    Rep Power

    Default 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(;
    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)
    System.out.println("Zero entered: Removing last item @ $" + priceList.get(i));
    subTotal -= priceArray [i];
    System.out.print("...your total is now $" + subTotal + "\n");
    else if (price >= 3000)
    System.out.println("INVALID PRICE: $" + price + " please re-enter...");

    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".

    Sorry for the wall of text and reading, thanks you your time if you read and/or answer to my question.

  2. #2
    Problemer is offline Member
    Join Date
    Sep 2012
    Rep Power

    Default 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. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. I don't get ArrayLists
    By Gio!? in forum New To Java
    Replies: 10
    Last Post: 05-03-2012, 12:50 PM
  2. Arraylists
    By talia in forum New To Java
    Replies: 5
    Last Post: 01-30-2012, 06:44 PM
  3. help with ArrayLists !
    By bigbreez21 in forum New To Java
    Replies: 3
    Last Post: 11-20-2011, 08:23 PM
  4. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 04:27 AM

Posting Permissions

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