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 25 of 25

Thread: Working with loops for the first time.

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

    Default Working with loops for the first time.

    Hello.

    So far, I am in my 3rd week of Java programming and I've finally run into a bit of a problem. We were given an assignment of the following:
    Scenario 1: Customer purchases all 9 items Please enter your name: Lisa Miller (This is what the sample output looked like)

    GRAPEFRUIT PRODUCT
    1. gPod shuffle $49
    2. gPod Touch $299
    3. gPad Mini $329
    4. gPad 2 $399
    5. gPhone $199
    6. gMac $1299
    7. MacNovel Pro $1199
    8. MacNovel Air $999
    9. MiniMac $599
    10. Complete my order

    Please select an item from the menu above: 5
    Please select anotheritem from themenu above: 2
    etc,

    Thank you for ordering with Grapefruit Company, Lisa Miller
    Total items ordered: 9
    Price of items ordered: $5371
    Sales tax: $ 349.115
    Total amount due: $ 5720.115

    package onlineordering;
     
    import java.util.Scanner;
     
    /**
     *
     * @author jamesbottom
     */
    public class OnlineOrdering {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            //Declare variables
            final int GPOD_SHUFFLE = 49;
            final int GPOD_TOUCH = 299;
            final int GPAD_MINI = 329;
            final int GPAD_2 = 399;
            final int GPHONE = 199;
            final int GMAC = 1299;
            final int MACNOVEL_PRO = 1199;
            final int MACNOVEL_AIR = 999;
            final int MINIMAC = 599;
            int nProductNumber = 0;
            int nSum = 0;
            Scanner input = new Scanner(System.in);        
     
            //Prompt user for name
            System.out.print("Please enter your name: ");
     
            //Get/read name 
            String name = input.next();
     
            //Create a line space
            System.out.println();
     
            //Display list of products for customer
            System.out.println("GRAPEFRUIT PRODUCT" + '\n' +
                    "1. gPod shuffle" + "   $" + GPOD_SHUFFLE + '\n' +
                    "2. gPod Touch" + "     $" + GPOD_TOUCH + '\n' +
                    "3. gPad Mini" + "      $" + GPAD_MINI + '\n' +
                    "4. gPad 2" + "         $" + GPAD_2 + '\n' +
                    "5. gPhone" + "         $" + GPHONE + '\n' +
                    "6. gMac" + "           $" + GMAC + '\n' +
                    "7. MacNovel Pro" + "   $" + MACNOVEL_PRO + '\n' +
                    "8. MacNovel Air" + "   $" + MACNOVEL_AIR + '\n'+
                    "9. MiniMac" + "        $" + MINIMAC + '\n' +
                    "10. Complete my order");
     
     
     
     
        }//end main method
    }//end class OnlineOrdering

    I don't understand what type of loop I should be doing. If anyone can point me in the right direction, I'd be really grateful.


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Working with loops for the first time.

    .
    Last edited by javaiscool; February 27th, 2013 at 03:01 AM. Reason: typos and clarification

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    what type of loop I should be doing
    That depends on what you want the loop to do and how many times you want to loop.

    The for loop is used mainly when you know how many times you want to loop. Like when going through the contents of a fixed size array.
    The while loop would be used when you don't know before starting the loop how many times to loop. Like when you are getting input from a user and are waiting for the user to say when it's time to exit the loop
    The do{}while loop is similar to the while except it will ALWAYS execute one time.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Working with loops for the first time.

    The customer can purchase as many items as they want. Each item corresponding to a number (1-9). The system will ask the user to enter another item until the user inputs 10. Then the total items ordered, price of items ordered, sales tax, and total amount due will be calculated.

    I dont understand how the computer can total each individual item as 1. So if a customer picks items 1, 5, 2, 6, and 7, how does the computer read it as a total of 5 items and not the mathematical sum (21).

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    how does the computer read it as a total of 5 items and not the mathematical sum (21).
    That depends on what the code does with the data:
    To count the number of items:
    counter++; // add one to count of items

    or to total their values:
    total = total + thisItem; // add the value of this item to the total
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Working with loops for the first time.

    .

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with loops for the first time.

    Ok update!
    I redid my coding and was able to fix the problem.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package orderfrommenu;
     
    import java.util.Scanner;
     
    /**
     *
     * @author jamesbottom
     */
    public class OrderFromMenu {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            final int SENTINEL = 10;    //Number used to indicate loop termination
            int nCount = 0;		//Number of integers entered by user
            int nSum = 0;			//Sum of numbers entered by user
            int nNumber = 0;    //Number entered by user
            //Declare variables
            final int GPOD_SHUFFLE = 49;
            final int GPOD_TOUCH = 299;
            final int GPAD_MINI = 329;
            final int GPAD_2 = 399;
            final int GPHONE = 199;
            final int GMAC = 1299;
            final int MACNOVEL_PRO = 1199;
            final int MACNOVEL_AIR = 999;
            final int MINIMAC = 599;
            Scanner input = new Scanner(System.in);
     
     
            //Prompt user for name
            System.out.print("Please enter your name: ");
            String first = input.next();
            String last = input.next();
     
            //Create a space
            System.out.println();
     
     
            //Display list of products for customer
            System.out.println("GRAPEFRUIT PRODUCT" + '\n' +
                    "1. gPod shuffle" + "   $" + GPOD_SHUFFLE + '\n' +
                    "2. gPod Touch" + "     $" + GPOD_TOUCH + '\n' +
                    "3. gPad Mini" + "      $" + GPAD_MINI + '\n' +
                    "4. gPad 2" + "         $" + GPAD_2 + '\n' +
                    "5. gPhone" + "         $" + GPHONE + '\n' +
                    "6. gMac" + "           $" + GMAC + '\n' +
                    "7. MacNovel Pro" + "   $" + MACNOVEL_PRO + '\n' +
                    "8. MacNovel Air" + "   $" + MACNOVEL_AIR + '\n'+
                    "9. MiniMac" + "        $" + MINIMAC + '\n' +
                    "10. Complete my order");
     
            //Create a space
            System.out.println();
     
            //Prompt user to select an item from the menu
            System.out.print("Please select an item from the menu: ");
     
            //Read number from user
            nNumber = input.nextInt();
     
            //while user entered more integers, calculate the sum and count the numbers entered
            while (nNumber != SENTINEL) {
                System.out.print("Please select another item from the menu: ");
                nNumber = input.nextInt();
     
                //Accumulate sum
                nSum = nNumber + nSum;
     
                //Increment count
                nCount++;
            } //end while user did not enter 10
     
            //Create a space
            System.out.println();
     
            //Print Thank You message
            System.out.println("Thank you for ordering with Grapefruit Company, " + first + " "+ last);
            //Print count of numbers entered
            System.out.println("Total Items Ordered: " + nCount);

    The only question I have now is, how would I calculate the price of the items selected.

    }//end main method
    }//end class OrderFromMenu

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    Do you know how to use arrays?
    Make an array with the prices such that using the product number as the index gets the price.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Working with loops for the first time.

    I don't know how to use arrays, or at the least, can't remember. Can you give an example?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    See the tutorial:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    or ask google

    or Search here on the forum
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    javaiscool (February 9th, 2013)

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

    Default Re: Working with loops for the first time.

    Thanks for the response. Our professor hasn't taught us about arrays so thats why it looks foreign to me.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package orderfrommenu;
     
    import java.util.Scanner;
     
    /**
     *
     * @author jamesbottom
     */
    public class OrderFromMenu {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            final int SENTINEL = 10;    //Number used to indicate loop termination
            int nCount = 0;		//Number of integers entered by user
            int nSum = 0;			//Sum of numbers entered by user
            int nNumber = 0;    //Number entered by user
            //Declare variables
            final int GPOD_SHUFFLE = 49;
            final int GPOD_TOUCH = 299;
            final int GPAD_MINI = 329;
            final int GPAD_2 = 399;
            final int GPHONE = 199;
            final int GMAC = 1299;
            final int MACNOVEL_PRO = 1199;
            final int MACNOVEL_AIR = 999;
            final int MINIMAC = 599;
     
            Scanner input = new Scanner(System.in);
     
     
            //Prompt user for name
            System.out.print("Please enter your name: ");
            String first = input.next();
            String last = input.next();
     
            //Create a space
            System.out.println();
     
     
            //Display list of products for customer
            System.out.println("GRAPEFRUIT PRODUCT" + '\n' +
                    "1. gPod shuffle" + "   $" + GPOD_SHUFFLE + '\n' +
                    "2. gPod Touch" + "     $" + GPOD_TOUCH + '\n' +
                    "3. gPad Mini" + "      $" + GPAD_MINI + '\n' +
                    "4. gPad 2" + "         $" + GPAD_2 + '\n' +
                    "5. gPhone" + "         $" + GPHONE + '\n' +
                    "6. gMac" + "           $" + GMAC + '\n' +
                    "7. MacNovel Pro" + "   $" + MACNOVEL_PRO + '\n' +
                    "8. MacNovel Air" + "   $" + MACNOVEL_AIR + '\n'+
                    "9. MiniMac" + "        $" + MINIMAC + '\n' +
                    "10. Complete my order");
     
            //Create a space
            System.out.println();
     
            //Prompt user to select an item from the menu
            System.out.print("Please select an item from the menu: ");
     
            //Read number from user
            nNumber = input.nextInt();
     
            //while user entered more integers, calculate the sum and count the numbers entered
            while (nNumber != SENTINEL) {
                System.out.print("Please select another item from the menu: ");
                nNumber = input.nextInt();           
     
                nSum = nNumber + nSum;    
     
                //Increment count
                nCount++;
     
            } //end while user did not enter 10
     
     
            //Create a space
            System.out.println();
     
            //Print Thank You message
            System.out.println("Thank you for ordering with Grapefruit Company, " + first + " "+ last);
            //Print count of numbers entered
            System.out.println("Total Items Ordered: " + nCount);
     
            //Print price
            System.out.println("Price of items ordered: " + nSum);
     
     
     
        }//end main method
    }//end class OrderFromMenu

    Heres the update to my code. I've managed to ask the user to input integers corresponding to the list given. The next thing I need to figure out is how to take each number and sum it for a price. In other words,

    if the user selects numbers 3,4,5,6,7.
    The computer recognizes it as 5 items purchased. (did that!)
    It sums the price of each product chosen based on the list (the issue)

    I emailed my teacher and she mentioned using if/else. I've tried that but its not working. Hope this helps.
    Last edited by Norm; February 9th, 2013 at 06:05 PM. Reason: added [ and ] to end tag

  13. #12
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Working with loops for the first time.

    .
    Last edited by javaiscool; February 27th, 2013 at 02:45 AM. Reason: typo

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    @javaiscool Please do not do OPs work for him.
    See: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Working with loops for the first time.

    B

  16. #15
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with loops for the first time.

    Sorry if I wasn't clear, but our Teacher hasn't taught us about array's yet. Therefore we should be required to figure this out without them.
    Thus, thats why I need to figure out the right combination of loops and if/elses.

  17. #16
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Working with loops for the first time.

    H

  18. #17
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with loops for the first time.

    @javaiscool. Thanks man, I think I'm on to something.

    I imported the code:
    if (nNumber == 1){
                nSum += GPOD_SHUFFLE;
            }
            else if (nNumber == 2){
                nSum += GPOD_TOUCH;
            }

    When I ran the program, i put in a 1 and a 2 to test it. The problem is it gave me the price of only item 1, without item 2. Any clues?

    In other words, if the user only wants item 1, the program will just give them the price of item 1. If they just want item 2, it will give them item 2. But I can't figure out how to give them the combination prices, i.e items 3,4,5,2,1,6 and not just one at a time.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    roblem is it gave me the price of only item 1, without item 2
    Can you post the full code that shows where the above bit of code is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with loops for the first time.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package orderfrommenu;
     
    import java.util.Scanner;
     
    /**
     *
     * @author jamesbottom
     */
    public class OrderFromMenu {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            final int SENTINEL = 10;    //Number used to indicate loop termination
            int nCount = 0;		//Number of integers entered by user
            int nSum = 0;			//Sum of PRICES entered by user
            int nNumber = 0;    //Number entered by user
            //Declare variables
            final int GPOD_SHUFFLE = 49;
            final int GPOD_TOUCH = 299;
            final int GPAD_MINI = 329;
            final int GPAD_2 = 399;
            final int GPHONE = 199;
            final int GMAC = 1299;
            final int MACNOVEL_PRO = 1199;
            final int MACNOVEL_AIR = 999;
            final int MINIMAC = 599;
     
            Scanner input = new Scanner(System.in);
     
     
            //Prompt user for name
            System.out.print("Please enter your name: ");
            String first = input.next();
            String last = input.next();
     
            //Create a space
            System.out.println();
     
     
            //Display list of products for customer
            System.out.println("GRAPEFRUIT PRODUCT" + '\n' +
                    "1. gPod shuffle" + "   $" + GPOD_SHUFFLE + '\n' +
                    "2. gPod Touch" + "     $" + GPOD_TOUCH + '\n' +
                    "3. gPad Mini" + "      $" + GPAD_MINI + '\n' +
                    "4. gPad 2" + "         $" + GPAD_2 + '\n' +
                    "5. gPhone" + "         $" + GPHONE + '\n' +
                    "6. gMac" + "           $" + GMAC + '\n' +
                    "7. MacNovel Pro" + "   $" + MACNOVEL_PRO + '\n' +
                    "8. MacNovel Air" + "   $" + MACNOVEL_AIR + '\n'+
                    "9. MiniMac" + "        $" + MINIMAC + '\n' +
                    "10. Complete my order");
     
            //Create a space
            System.out.println();
     
            //Prompt user to select an item from the menu
            System.out.print("Please select an item from the menu: ");
     
            //Read number from user
            nNumber = input.nextInt();
     
            if (nNumber == 1){
                nSum += GPOD_SHUFFLE;
            }
            else if (nNumber == 2){
                nSum += GPOD_TOUCH;
            }
     
            //while user entered more integers, calculate the sum and count the numbers entered
            while (nNumber != SENTINEL) {
                System.out.print("Please select another item from the menu: ");
                nNumber = input.nextInt();           
     
     
                //Increment count
                nCount++;
     
            } //end while user did not enter 10
     
     
     
            //Create a space
            System.out.println();
     
            //Print Thank You message
            System.out.println("Thank you for ordering with Grapefruit Company, " + first + " "+ last);
            //Print count of numbers entered
            System.out.println("Total Items Ordered: " + nCount);
     
            //Print price
            System.out.println("Price of items ordered: " + nSum);
     
     
     
        }//end main method
    }//end class OrderFromMenu

    * I noticed it only works if I keep it in that area of the code. If I put it in the while loop, it does nothing. Also, I just put the 2 products as a test. I know I will eventually have to put all of them.

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    What does the code inside the while loop do?
    Where do you add the price of the item to the total?
    If you want the add more prices to the total, you need to have code to do that after the item number is entered.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with loops for the first time.

    The while loop asks the user to enter a number for a product. Until they enter the integer 10 (the sentinel), it will keep asking them for a product number.

    ex.
    Please select an item from the menu: 5
    Please select another item from the menu: 3
    ....
    and so forth until they hit 10.

    One of my teacher assistants just suggested I put the println for all the products into the while loop. I dont see how that makes sense because I don't want that list to be regenerated every time the user puts in an integer.

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    Where do you add the price of the item to the total?
    If you want the add more prices to the total, you need to have code to do that after the item number is entered.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with loops for the first time.

    Sorry to sound like a pain but thats the problem I'm having. I honestly have no idea how to move on.

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with loops for the first time.

    Move the code that gets the price of the entered item (post#16) INSIDE the loop so it follows the code that gets the item number from the users.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Working with loops for the first time.

    .

Similar Threads

  1. Hello, first time caller long time programmer....
    By P2C2N in forum Member Introductions
    Replies: 3
    Last Post: December 10th, 2012, 11:53 AM
  2. Replies: 2
    Last Post: October 19th, 2012, 03:32 AM
  3. Replies: 6
    Last Post: October 3rd, 2012, 06:39 PM
  4. Playing sound each time mouse is clicked (not working)
    By BestSanchez in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 24th, 2012, 09:51 PM
  5. [SOLVED] Boolean Flag Not Working to Move Between While Loops?
    By verbicidalmaniac in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 8th, 2011, 09:04 PM

Tags for this Thread