Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Java Cupcake Counter Help

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Cupcake Counter Help

    Hello everyone,

    I am in need of help in creating loop for the following scenario. I am new to java and have I am currently stuck on this one.


    1 - one individual cupcake sold (for $2)
    3 - a "package of 3" sold (for $5)
    1 - one individual cupcake sold (for $2)
    6 - a "package of 6" sold (for $8)

    Build a program that will accept the input of each sale. Use a loop that will keep
    accepting input until a sentinel value of 0 (zero) is entered. Ignore any input that is not 1 or 3 or 6; use
    an if or select to accomplish this. While in the loop, you will accumulate the following three values:

    * Total amount of dollar sales. For the input (what package size was sold), use an if or select to
    determine the appropriate dollar sales to add. The prices are above; for example, if "3" is
    entered, add $5 to the Total amount of dollar sales.
    * Total number of cupcakes sold. Add the appropriate number; for example, if "6" is entered, add
    6 to the Total number of cupcakes sold.
    * Number of multi-cupcake packages sold. If the input package size sold is larger than 1, add one
    to this counter. (This will accumulate "we sold 2 multi-cupcake packages", not count how many
    cupcakes were in those packages.) Use an if or select to accomplish this.


    Here is what I currently have

    public static void main(String[] args) {
     
            //Initialize Variables
            int totalDollarSales = 0;
            int numberCupcakesSold = 0;
            int countMultiCupcakePackages = 0;
            int oneCupcake = 3;
            int threeCupcake = 5;
            int sixCupcake = 8; 
     
            //Create a Scanner for Input
            Scanner input = new Scanner(System.in);  
     
            //Loop cup cake counter
     
     
            //Read an initial input
            System.out.println("Enter package size sold: 1, 3, or 6: ");
            int data = input.nextInt();
     
     
            //Keep reading data until the input is 0
            int sum = 0;
            while (data !=0) {
                sum += data;
     
                //Read the next input
                System.out.println("Enter package size sold - 1, 3, or 6: ");
                data = input.nextInt();
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,081
    Thanks
    47
    Thanked 2,149 Times in 2,122 Posts

    Default Re: Java Cupcake Counter Help

    Before adding any more code, fix the current code so it compiles and executes without any errors.
    When it works, then move on to adding a loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Java Cupcake Counter Help

    The way you have it written in that snippet will not compile without errors, although the fix is very minor and may have just been from not copying and pasting all of your code. There is also an error in that your code doesn't accurately model the situation you were given. Take a look at the value for int oneCupcake. Once these fixes are done, the rest of your code has to go in your loop with just a few if-else statements and updating the 3 field variables to be changed. Optionally, to make the function of the field variables a bit clearer, think which ones will never have their value changed and make them a constant using the keyword, "final" with the appropriate name changes.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Cupcake Counter Help

    I currently have this

      public static void main(String[] args) {
     
            //Initialize Variables
            int totalDollarSales = 0;
            int numberCupcakesSold = 0;
            int countMultiCupcakePackages = 0;
            int oneCupcake = 3; 
            int threeCupcake = 5;
            int sixCupcake = 8;
     
     
            //Create a Scanner for Input
            Scanner input = new Scanner(System.in);  
     
     
     
            //Read an initial input
            System.out.println("Enter package size sold: 1, 3, or 6: ");
            int data = input.nextInt();
     
     
            //Keep reading data until the input is 0
              while (data !=0){
                  if ( data == 1 ) {
                    totalDollarSales = 3; 
                    numberCupcakesSold = 1;
                    countMultiCupcakePackages = 0;
                  }
                  else if ( data == 3 ) {
                    totalDollarSales = 5;
                    numberCupcakesSold = 3;
                    countMultiCupcakePackages = 1;
                  }
                 else if ( data == 6 ) {
                    totalDollarSales = 8;
                    numberCupcakesSold = 6;
                    countMultiCupcakePackages = 1;
                 }
                //Read the next input
     
                System.out.println("Enter package size sold - 1, 3, or 6: ");
                data = input.nextInt();
     
              //Variables
     
     
            }
     
     
            System.out.println("Total Dollar Sales is " + );
            System.out.println("Total Cupcakes Sold is " + );
            System.out.println("Multi Cupcake Packages is " + );
     
        }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,081
    Thanks
    47
    Thanked 2,149 Times in 2,122 Posts

    Default Re: Java Cupcake Counter Help

    What you have posted won't compile. Its missing a class statement and the import statements.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Cupcake Counter Help

    I know it doesn't work. I am new to java... thats the reason I posted here for tips and help.

    Thanks

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,081
    Thanks
    47
    Thanked 2,149 Times in 2,122 Posts

    Default Re: Java Cupcake Counter Help

    I know it doesn't work
    please copy and paste here the full text of the compiler's error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Cupcake Counter Help

    Norm I am getting the message:

    "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at cupcakecounter.java.CupCakeCounterJava.main(CupCak eCounterJava.java:72)
    Java Result: 1"

    It is because I have not entered variables for the:
    System.out.println("Total Dollar Sales is " + );

    System.out.println("Multi Cupcake Packages is " + );


    That said here is my current code.

        public static void main(String[] args) {
     
            //Initialize Variables
            int totalDollarSales = 0;
            int numberCupcakesSold = 0;
            int countMultiCupcakePackages = 0;
            int oneCupcake = 3; 
            int threeCupcake = 5;
            int sixCupcake = 8;
     
     
            //Create a Scanner for Input
            Scanner input = new Scanner(System.in);  
     
     
     
            //Read an initial input
            System.out.println("Enter package size sold: 1, 3, or 6: ");
            int data = input.nextInt();
     
     
            //Keep reading data until the input is 0
            int sum = 0;  
            while (data !=0){
                  if ( data == 1 ) {
                    totalDollarSales = 3; 
                    numberCupcakesSold = 1;
                    countMultiCupcakePackages = 0;
                  }
                  else if ( data == 3 ) {
                    totalDollarSales = 5;
                    numberCupcakesSold = 3;
                    countMultiCupcakePackages = 1;
                  }
                 else if ( data == 6 ) {
                    totalDollarSales = 8;
                    numberCupcakesSold = 6;
                    countMultiCupcakePackages = 1;
                 }
                //Get user input
                sum += data;  
     
     
     
                //Read the next input
     
                System.out.println("Enter package size sold - 1, 3, or 6: ");
                data = input.nextInt();
     
            }
     
     
            System.out.println("Total Dollar Sales is " + );
            System.out.println("Total Cupcakes Sold is " + sum);
            System.out.println("Multi Cupcake Packages is " + );
     
        }
    }

    I believe that "System.out.println("Total Cupcakes Sold is " + sum);" is correct and will give me the correct outcome. I just need help on the other two.

    Thanks

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,081
    Thanks
    47
    Thanked 2,149 Times in 2,122 Posts

    Default Re: Java Cupcake Counter Help

    Can you post the errors from the compiler. What you posted looks like it is from trying to execute the code.
    You need to find and fix the compiler errors before trying to execute the program.

    The posted code is missing the import statement(s)
    and the class statement. What is posted will not compile.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Cupcake Counter Help

    72 and 74 state "illegal state of expression" if that is what you are asking.

    My problem is I do not know how to express the correct variables together for example to find out the total Sales...

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,081
    Thanks
    47
    Thanked 2,149 Times in 2,122 Posts

    Default Re: Java Cupcake Counter Help

    You need to find out how to get a complete list of the compiler's error messages so you can post them.
    A one line message doesn't help much.
    Here is a sample of an error message from the javac compiler program:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    Did you see this? This is one problem:

    The posted code is missing the import statement(s)
    and the class statement. What is posted will not compile.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Cupcake Counter Help

    Thank you for the help, I figured it out finally.

  13. #13
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Cupcake Counter Help

    Quote Originally Posted by Hawks View Post
    Thank you for the help, I figured it out finally.
    Can you post your final code? I need help with this problem as well and I would like to compare.

Similar Threads

  1. (Counter/Accessor) Did I do this right?
    By skw4712 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 3rd, 2012, 04:57 PM
  2. [SOLVED] Counter
    By mssim in forum Java Theory & Questions
    Replies: 2
    Last Post: November 7th, 2012, 04:32 AM
  3. Counter troubles
    By GinoGore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 09:28 AM
  4. Help With Counter
    By Catgroove in forum Java Theory & Questions
    Replies: 7
    Last Post: February 10th, 2011, 07:50 AM
  5. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 10:49 AM