Thread: Trouble effectively working with arrays

1. Senior Member
Join Date
Jan 2014
Posts
106
Rep Power
0

Trouble effectively working with arrays

I need to create a program that will enable me to do 3 things: 1. Allow the user to enter "x" number of girls into an array (girlScouts) 2. Record the number of boxes sold by each girl within the troop. 3. Keep a tally of boxes sold for predetermined ranges (i.e. 1-10, 11-20, 21-30..etc).

There has to be a better way of keeping a tally than saleCount (program below). Just not sure how else to accomplish it.

How do I assign the number of boxes each girls sells to a table of ranges defined earlier? Should I create an array to build the table of ranges?

"The program should use an array of counters to keep track of the number of Girl Scouts selling boxes of cookies in each of these ranges. The number of boxes sold for each Girl Scout will be entered at the keyboard. The total number of girls in the troop selling cookies will also be entered at the keyboard. After the total boxes for each girl in the troop has been entered, the resulting statistics for the number of boxes should be printed out to the console in the following format:"
**---as stated in the assignment ----**

Java Code:
```import java.util.Scanner;

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){
}
else if(count > 11 && count <=20){
}
else if(count < 21 && count <= 30){
int
}

}

}```

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,926
Rep Power
10

Re: Trouble effectively working with arrays

As I understand the problem, you should first read in the number of girl scouts selling cookies. You can use that to allocate your array.
Then just read in the boxes sold for each girl scout. Once you get that done, verify you did it correctly by iterating over the array and printing out each item.

Then for part two, it appears you need to keep a tally of how many girl scouts sold boxes within a range. For example, I would assume if you had the following input:
4
10
14
9
30

The first would be the number of girls selling cookies. Then I would expect that to be two girls sold in range 1-10, one girl in range 11-20 and one girl in range 21-30. That can be done by another array and some if/else clauses.

Of course, this is all based on my interpretation of the assignment.

Regards,
Jim

3. Senior Member
Join Date
Jan 2014
Posts
106
Rep Power
0

Re: Trouble effectively working with arrays

Thank you for your reply Jim. How would you recommend associating the number of cookies sold with the specific girl? Should I have the user enter the girls name and then the number of boxes sold by that girl before moving on to the next girl? If so, should I double the size of the array, since I have based its size off the number of girls....where say, Ashley (array [0[) sold 25 boxes (array [1]), then Micah (array[3]) sold 10 boxes (array[4])? My confusion is how to enter the number of boxes sold, save that, then go through and keep a tally for the table. I hope that I am being clear enough, if not, let me know and I will try to explain it more thoroughly. Thanks!!

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,926
Rep Power
10

Re: Trouble effectively working with arrays

Well, assuming you are asked to use arrays, have an array for the girls names and an array for the number of boxes sold by each girl. So in all cases a specific index, say 4, would index into the fifth girl (remember arrays start at 0) and into the fifth slot for boxes sold. You simply read these via the terminal and enter them together. I am assuming that a given girls name and number of boxes is only entered once.

The number of boxes sold in each range is independent of the girls who sold them. If you have 5 ranges, you need an array of 5. Then just iterate thru the boxes sold and when you detect it is within the range, increment the count.

Regards,
Jim

5. Re: Trouble effectively working with arrays

The term I've heard and used for using arrays like Jim has described is "parallel arrays".

6. Senior Member
Join Date
Jan 2014
Posts
106
Rep Power
0

Re: Trouble effectively working with arrays

Is it preferred to use a parallel array, instead of using two different arrays? It seems to me there would be less room for error this way. Would it be possible to cycle through the parallel array to determine the number of boxes sold in order to create the tally?

Also, there is an error with the String [] girlScout, something about the array dimension and incompatible types error...do you know why that is?

7. Senior Member
Join Date
Jan 2014
Posts
106
Rep Power
0

Re: Trouble effectively working with arrays

Yes, Norm, that is what I was thinking. So it is possible to have a parallel array that contains strings and integers? Is it also possible to scan that parallel array of integers to create a table, which I would create within its own method using if/else statements.

*EDIT*

It appears to me after looking at the book that this is not possible. A multidimensional array must be of the same type. It seems I will need to create two separate arrays.
Last edited by javaStooge; 02-09-2014 at 06:24 PM.

8. Re: Trouble effectively working with arrays

parallel array, instead of using two different arrays
The term is parallel arrays. That means more than one array with each array holding a different piece and type of data.
Normally in OOP there would be a class that holds the different pieces of data and instances of that class would be kept in a collection like an ArrayList. I assume your class will move on to that technique later on.

9. Senior Member
Join Date
Jan 2014
Posts
106
Rep Power
0

Re: Trouble effectively working with arrays

You would be right. Thanks for the clarification. I will take a look at that info again and tie the two arrays in together. This should help me get started. Thanks guys.

10. Re: Trouble effectively working with arrays

Posting Permissions

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