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

Thread: Arrays - Creating A List - Cookie Sales

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Arrays - Creating A List - Cookie Sales

    I am starting a program that is dealing with arrays. My issue, allowing a user to enter 'x' number of people into an array. How do I first allow them to enter the size of an array, and then enter strings (list) into that array. In addition to this, I need to specify the number of boxes of cookies sold by each person and keep a tally of the number of people selling cookies in each category (e.g. 1-10, 11-20...etc). Instead of having a number 100000, I want the user to enter that size, 'x'. I am still trying to piece it together, so please forgive the mess.

    import java.util.Scanner;
     
    public class cookie {
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int x = 100;
            double saleCount[] = new double [x];
            String[] girlScout = new String[100000];
     
           System.out.print("Enter the number of scouts in your troop: ");       
           int[] troopSize = new int[input.nextInt()];
     
           for( int i = 0 ; i < troopSize.length ; i++ ){ 
               System.out.print( "Enter scout name number" + (i+1) + ": " ); 
               //String[] girlScout = new String[i];
               girlScout[i] = input.next(); 
               System.out.println(girlScout[i]);
                } 
           //for(int i; i < girlScout.length; i++)
           //System.out.println("Test: " + girlScout[i] + " ");
     
     
            //for(input = 0 ; i <= input  ; i++){
     
           //}
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Arrays - Creating A List - Cookie Sales

    If I understand where you're going, this method of keeping track of data is known as "parallel arrays." You'll have an array of people and and array of numbers of boxes and the two arrays elements match, people[0] is the name of the person and boxes[0] is the number of boxes people[0] bought. In an OOP language like Java, using parallel arrays is old school, undesirable, and sucky. Do you have to use parallel arrays, or is it the design you thought of?

    An alternative is to create a class (maybe more), like Customer. Customer would have the necessary fields, name, address, phoneNumber to identify the "person" aspect. Another class, maybe Cookie, could be used to define the cookie flavor and a number of boxes. Then customer could also include a collection of Cookies to keep track of what kind and how many of each cookie type each Customer ordered.

    If you're stuck using parallel arrays, what questions do you have about that? If you're not, create classes instead.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Arrays - Creating A List - Cookie Sales

    Lol, it is the design I thought of. I wasn't sure of any other way to associate the girl with the cookies she sold. Are you referring to class arrays, or just classes? We have not covered the topic of classes yet. Is the class array a better alternative than the approach I've chosen above?

    Sidenote: Regarding my posts, is there a desired format moderators prefer (i.e. assignment criteria, post layout..etc)? I'm new to forum posts and would like to get my questions/concerns across clearly.

    Thank you for the guidance and keeping me on the right path. It's an online class, it has its positives and negatives--mostly negatives in this case.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Arrays - Creating A List - Cookie Sales

    There are probably as many preferred ways to post as their members, so don't worry about that. Just be clear when you explain what help you need.

    All right. If you haven't learned creating classes yet besides one to hold a main() method where all of your code will be, then it must be too soon for that approach. Sometimes parallel arrays are used as a stepping stone to show what a sucky approach they are and what an advantage the class approach is.

    As I described and as you've already thought, create the arrays you need, fill them with the necessary items in the correct places, and figure out how to keep it all straight. Either size the arrays as large as the largest Girl Scout troop in the world or ask the user to specify a size and create all arrays to that size. Not sure why you need help with that part.

  5. #5
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Arrays - Creating A List - Cookie Sales

    I'm trying to find a way of recording the count of cookie sales by each girl compressed. This is to say that, each girl will be divided into groups that are based upon of the number of boxes each has sold ( 1-10, 11 - 20, 21 - 30..etc). Is there a better way of doing this? I am considering a method that is constructed of if-else if statements to add a specific girl to each category, based on how many boxes they have sold. I know that when we see the same code being written over and over, we should consider a more efficient way of coding.

    I would like the method saleCount to run in after girlScouts[] has completed, read the number of sales associated with that girl and keep a tally of the number of girls for each category of boxes sold. In other words, the user enters the girls name, then the number of cookie boxes that girl sold, after they've all been enter, I would like the saleCount method to run, cycle through each girls number of boxes sold and keep a tally.

    Also there is an error in the girlScout method, the String[] girlScout, is missing an array dimension...?!? I would like to to be equal in size to the int size variable.



    import java.util.Scanner;
     
    public class cookie {
     
        Scanner input = new Scanner(System.in);
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
             //double saleCount[] = new double [x];    
           System.out.print("Enter the number of scouts in your troop: ");       
                  int size = input.nextInt();
                  int[] troopSize = new int[size];
           girlScout();
     
        }
     
        public void girlScout(int troopSize[]){
            for(int i = 0 ; i < troopSize.length; i++){
                System.out.print("Enter scout name " + (i+1) + ": ");
                String[] girlScout = new String[]; 
                System.out.println(girlScout[i]);
            }
        }   
     
        public void saleCount(int count){
            if(count > 0 && count <= 10){
                int cookieOne += 1;
            }
            else if(count > 11 && count <=20){
                int cookieTwo += 0;
            }
            else if(count < 21 && count <= 30){
                int
            }    
     
        }
     
    }

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Arrays - Creating A List - Cookie Sales

    A better way? Yes, but we've already discussed that, and it was rejected.

    Do not use variables like cookieOne and cookieTwo ever, but especially when you don't know the bounds. Will there be a cookie10, cookie100?

    I don't understand what grouping the girls by 10s of boxes sold accomplishes. You can compute the groups on the fly and output the results, but saving that information is not useful.

  7. #7
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Arrays - Creating A List - Cookie Sales

    I'm not sure what else I can do besides if/else statements to scan through the array and make a tally for the defined ranges...? We need to create a table that marks a tally, in a specific range of boxes sold by each girl. So, of the 4 girls in the troop, 1 of them sold 0-10 boxes and 2 of them sold 11-20 boxes and 1 of them sold 21-30.

    Name.....Boxes
    Ashley.......4
    Micah.......13
    Kristin......25
    Betty.......18

    I then need to cycle through that array of boxes sold to create the table of ranges. That was my purpose in using cookeOne.
    Sample output:

    Range of Boxes......Number of girl scouts
    0 - 10 ...........................1
    11 - 20 .........................2
    21 - 30 .........................1

    *Please ignore the periods, needed to do in order to format for post.

  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: Arrays - Creating A List - Cookie Sales

    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Arrays - Creating A List - Cookie Sales

    You are good Norm, haha. I am hoping to get a broader response to help figure this out. Jim said I should use if/else statements to create the table, but Greg seems to think there is a different approach---which I am unaware of...

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Arrays - Creating A List - Cookie Sales

    My different approach was to use classes rather than parallel arrays. Now that you're locked into the parallel array design, if/then logic could be a tool to categorize the data stored in the arrays. As I said early on, keeping the data straight or lined up is a constant challenge in this design, and the more ways you slice the data, resulting in another parallel array, the more challenging it becomes.

    As much as it pains me to support a parallel array design, there is another approach: Create a frequency array to record the numbers of boxes sold and then collapse the frequency array to the desired ranges.

Similar Threads

  1. Creating methods with Arrays
    By incxx in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 23rd, 2013, 08:44 PM
  2. Arraylist of averages of two arrays list
    By Hazmat210 in forum Collections and Generics
    Replies: 11
    Last Post: April 5th, 2012, 07:38 PM
  3. Arrays and array list
    By rob17 in forum Collections and Generics
    Replies: 2
    Last Post: February 19th, 2012, 06:10 PM
  4. Merging Arrays Vs. Linked List.
    By Kumarrrr in forum Collections and Generics
    Replies: 1
    Last Post: March 1st, 2010, 03:20 AM