Tuan Nguyen
Tuan Nguyen

Reputation: 197

Help with Arrays for Java

// ****************************************************************
// Sales.java
//
// Reads in and stores sales for each of 5 salespeople.  Displays
// sales entered by salesperson id and total sales for all salespeople.
//
// ****************************************************************
import java.util.Scanner;

public class Sales
{
    public static void main(String[] args)
    {
    final int SALESPEOPLE = 5;
    int[] sales = new int[SALESPEOPLE];
    int sum;
    int average;
    int max=sales[0];
    int min=sales[0];
    int salemade;
    Scanner scan = new Scanner(System.in);

    for (int i=0; i<sales.length; i++)
        {
        System.out.print("Enter sales for salesperson " + (i+1) + ": ");
        sales[i] = scan.nextInt();
        }
    // Find maximum and minimum sale value   
    for (int i=0; i<sales.length; i++)

        if(sales[i]>max)
        max=sales[i];
        System.out.println("Salesperson "+sale[i]+ "has the max sale of $ " + max);  
        if(sales[i]<min)
        min=sales[i];
    System.out.println("Salesperson "+sale[i]+ "has the min sale of $ "+min);
// List of sales
    System.out.println("\nSalesperson   Sales");
    System.out.println("--------------------");
    sum = 0;


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

        average= sum/5;
    System.out.println("\nTotal sales: " + sum);
    System.out.println("The average sale is:$ "+average);

    //See who exeeded the max sale
    System.out.println("Enter the amount of sale made");
    salemade=scan.nextInt();

    for(int i=0; i=sales.length; i++)
        {
        if(sales[i]>=salemade)
        System.out.println(sales[i]);
        }

    }
}

I'm trying to write this program that asked the user to put in the amount of sales of 5 people, and it will display the max and min value and print out the sale person id and amount of min and max sale("Salesperson 3 had the highest sale with $4500.") after the program prints the min, max and average, it will ask the user to enter a random number and compares it to those previous number and see who met or exeeded the max amount and print that person id. I'm having trouble getting it done.Can some one take a look and help me? These are the error i got

Sales.java:33: cannot find symbol
symbol  : variable sale
location: class Sales
        System.out.println("Salesperson "+sale[i]+ "has the max sale of $ " + max);  
                                          ^
Sales.java:33: cannot find symbol
symbol  : variable i
location: class Sales
        System.out.println("Salesperson "+sale[i]+ "has the max sale of $ " + max);  
                                               ^
Sales.java:34: cannot find symbol
symbol  : variable i
location: class Sales
        if(sales[i]<min)
                 ^
Sales.java:35: cannot find symbol
symbol  : variable i
location: class Sales
        min=sales[i];
                  ^
Sales.java:36: cannot find symbol
symbol  : variable sale
location: class Sales
    System.out.println("Salesperson "+sale[i]+ "has the min sale of $ "+min);
                                      ^
Sales.java:36: cannot find symbol
symbol  : variable i
location: class Sales
    System.out.println("Salesperson "+sale[i]+ "has the min sale of $ "+min);
                                           ^
Sales.java:57: incompatible types
found   : int
required: boolean
    for(int i=0; i=sales.length; i++)
                  ^
7 errors

Upvotes: 1

Views: 4225

Answers (6)

Aleadam
Aleadam

Reputation: 40401

first, you forgot the braces in the line

for (int i=0; i<sales.length; i++)

so you need to make it

int posMax = 0, posMin=0;
for (int i=0; i<sales.length; i++) {
    if(sales[i]>max) {
        posMax = i;
        max=sales[i];
    }
    if(sales[i]<min) {
       posMin = i;
       min=sales[i];
    }
}
System.out.println("Salesperson "+ posMax + " has the max sale of $ " + max);  
System.out.println("Salesperson "+ posMin + " has the min sale of $ "+min);

and here (line 57)

for(int i=0; i=sales.length; i++)

you need to compare, not assign

for(int i=0; i==sales.length; i++)

Upvotes: 1

Feanor
Feanor

Reputation: 2725

Line 33: There is no array named sale; your array is called sales.

Line 33, 34: The variable i is out of scope at this point. You should probably use brackets for the if statement and for loop. Java allows you to do an if without brackets, but it can confuse the compiler if the scope isn't clear.

When debugging compile problems, you should read the output and see which line numbers are causing problems. If the compiler can't find a symbol, it probably means your reference is out of scope or not there (for example, misspellings).

Upvotes: 0

GuruKulki
GuruKulki

Reputation: 26428

try with code:

// ****************************************************************
// Sales.java
//
// Reads in and stores sales for each of 5 salespeople.  Displays
// sales entered by salesperson id and total sales for all salespeople.
//
// ****************************************************************
import java.util.Scanner;

public class Sales {
    public static void main(String[] args) {
        final int SALESPEOPLE = 5;
        int[] sales = new int[SALESPEOPLE];
        int sum;
        int average;
        int max = sales[0];
        int min = sales[0];
        int salemade;
        Scanner scan = new Scanner(System.in);

        for (int i = 0; i < sales.length; i++) {
            System.out.print("Enter sales for salesperson " + (i + 1) + ": ");
            sales[i] = scan.nextInt();
        }
        // Find maximum and minimum sale value
        for (int i = 0; i < sales.length; i++) {
            if (sales[i] > max)
                max = sales[i];
            System.out.println("Salesperson " + sales[i]
                    + "has the max sale of $ " + max);
            if (sales[i] < min)
                min = sales[i];

            System.out.println("Salesperson " + sales[i]
                    + "has the min sale of $ " + min);
        }
        // List of sales
        System.out.println("\nSalesperson   Sales");
        System.out.println("--------------------");
        sum = 0;

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

        average = sum / 5;
        System.out.println("\nTotal sales: " + sum);
        System.out.println("The average sale is:$ " + average);

        // See who exeeded the max sale
        System.out.println("Enter the amount of sale made");
        salemade = scan.nextInt();

        for (int i = 0; i < sales.length; i++) {
            if (sales[i] >= salemade)
                System.out.println(sales[i]);
        }

    }
}

Upvotes: 0

camiloqp
camiloqp

Reputation: 1140

You also need to fix your last for cycle to the following:

for(int i=0; i<sales.length; i++)
    {
       if(sales[i]>=salemade)
       System.out.println(sales[i]);
    }

Upvotes: 1

Umesh Kacha
Umesh Kacha

Reputation: 13686

Compilation errors are coming because you misspelled Sales[] with Sale[] in print statements.

Upvotes: 0

unholysampler
unholysampler

Reputation: 17341

int[] sales = new int[SALESPEOPLE];
...
System.out.println("Salesperson "+sale[i]+ "has the max sale of $ " + max);  

You defined the array as sales, but then try to reference it as sale.

Upvotes: 5

Related Questions