# Thread: I need some help with this code I am writing:

1. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## I need some help with this code I am writing:

I have been working on this assignment and this is what I have so far, I am stuck now and i cannot figure out what to change so that it prints correctly.

here are the given directions:
1. Compute and print the average sale. (You can compute this directly from the total; no loop is necessary.)
2. Find and print the maximum sale. Print both the id of the salesperson with the max sale and the amount of the sale, e.g., "Salesperson 3 had the highest sale with \$4500." Note that you don't need another loop for this; you can do it in the same loop where the values are read and the sum is computed.
3. Do the same for the minimum sale.
4. After the list, sum, average, max and min have been printed, ask the user to enter a value. Then print the id of each salesperson who exceeded that amount, and the amount of their sales. Also print the total number of salespeople whose sales exceeded the value entered.
5. The salespeople are objecting to having an id of 0—no one wants that designation. Modify your program so that the ids run from 1–5 instead of 0–4. Do not modify the array—just make the information for salesperson 1 reside in array location 0, and so on.
6. Instead of always reading in 5 sales amounts, at the beginning ask the user for the number of sales people and then create an array that is just the right size. The program can then proceed as before.

this is the code I have so far:

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

public class Sales
{
public static void main(String[] args)
{
int SALESPEOPLE = 0;
int[] sales = new int[SALESPEOPLE];
int sum;
int avgSale;
int maxID = 0;
int minID = 0;
int max = 0;
int min = 0;

NumberFormat fmt = NumberFormat.getCurrencyInstance();
Scanner scan = new Scanner(System.in);

System.out.println("Enter number of sales people: ");
SALESPEOPLE = scan.nextInt();

for (int i=1; i<sales.length; i++)
{
System.out.print("Enter sales for salesperson " + i + ": ");
sales[i] = scan.nextInt();
}

System.out.println("\nSalesperson   Sales");
System.out.println("--------------------");
sum = 0;
for (int i=0; i<sales.length; i++)
{
System.out.println("     " + i + "         " + sales[i]);
sum += sales[i];
}

System.out.println("\nTotal sales: " + sum);

avgSale = (sum / SALESPEOPLE);

System.out.println("Average Sale: " + avgSale);

System.out.println("Salesperson " +maxID + "had the highest sale with " +fmt.format(max));
System.out.println("Salesperson " +minID + "had the highest sale with " +fmt.format(min));

}
}```
This is what i get when attempting to run the program:

----jGRASP exec: java Sales

Enter number of sales people:
5

Salesperson Sales
--------------------

Total sales: 0
Average Sale: 0
Salesperson 0had the highest sale with \$0.00
Salesperson 0had the highest sale with \$0.00

----jGRASP: operation complete.

2. ## Re: I need some help with this code I am writing:

Regarding this code:

Java Code:
```    int SALESPEOPLE = 0;
int[] sales = new int[SALESPEOPLE];```
How big will this array be do you think? What is its length?

Also, please follow Java naming conventions. Variables should be camel-cased and the first letter should be lower-case. Only constants should be all upper-case with underscores between words.

3. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: I need some help with this code I am writing:

I am supposed to ask the user to enter a number for this value and then have it loop until the person says they are done.

4. ## Re: I need some help with this code I am writing:

Originally Posted by loftus727
I am supposed to ask the user to enter a number for this value and then have it loop until the person says they are done.
But you didn't answer my question.

5. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: I need some help with this code I am writing:

The array will be as big as the number the person enters, if this isnt the answer you are looking for then I do not understand your question....

6. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: I need some help with this code I am writing:

If this isnt the answer youre looking for can you further explain what youre asking?

7. ## Re: I need some help with this code I am writing:

You set your array size in the code I've shown you:

Java Code:
```int SALESPEOPLE = 0;
int[] sales = new int[SALESPEOPLE];```
To simplify it even further, your code above is essentially this:

Java Code:
`int[] sales = new int[0];`
Now, how big is this array? Hint: it's a number that you see in one of the lines above in this very post.

8. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: I need some help with this code I am writing:

I am gonna go with zero?

9. ## Re: I need some help with this code I am writing:

Originally Posted by loftus727
I am gonna go with zero?
And you are correct.

So how will this for loop behave if sales.length returns 0?
Java Code:
```    for (int i=1; i<sales.length; i++) {
System.out.print("Enter sales for salesperson " + i + ": ");
sales[i] = scan.nextInt();
}```

10. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: I need some help with this code I am writing:

everything would probably just come out to equal zero?

so to change that I would have to change what salesperson is equal too?
I tried to have it this way
Java Code:
`int SALESPEOPLE;`
but then it came up saying that it wasnt initialized, and then i tried to set it too

Java Code:
`int SALESPEOPLE=1;`
then it came up with everything equaling zero again like before.

11. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
10

## Re: I need some help with this code I am writing:

Try a different number and see what happens .

Regards.

12. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: I need some help with this code I am writing:

how would i go about setting it to having the user enter a number? that is what I am trying to get it to do. I can not figure it out at all.
I tried and it went to saying it wasnt initialized and then i got stuck.

13. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
10

## Re: I need some help with this code I am writing:

Your probelm is on line 9 where sales is declared and initialized. Instead, declare the array on line 9 and leave the initilization until say line 22, after the variable 'SALESPEOPLE' has been set.

It is also worth changing 'SALESPEOPLE' to lowercase as capitals are generally reserved for variables declared as final. Its not compulsary, just conventional coding practice and makes it easier to tell the difference.

Regards.

14. Member
Join Date
Feb 2013
Posts
24
Rep Power
0

## Re: I need some help with this code I am writing:

if i declare it as a number than it only works for that number, if you dont mind could you possibly explain to me a little more how an array works?
ex. declaring and initializing

15. ## Re: I need some help with this code I am writing:

Please have a look at the Oracle tutorial on Java arrays, a site which you can easily find on Google if future need arises.

#### Posting Permissions

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