Link to home
Start Free TrialLog in
Avatar of Cynthia Hill
Cynthia HillFlag for United States of America

asked on

Java Newbie - Array not holding separate values

Hello - I'm a Java newbie currently taking a class.  My assignment from last week was to create code with an array, with prompts, etc.  

The assignment has already been turned in as is...I am just trying to further my learning to see where I went wrong (it's an on-line course and the instructor does not give very detailed feedback on the code).  

The code prompts for info such as sales person names and annual sales amounts and then calculates the total compensation.  However, the prompts do not hold the separate values.  The total compensation returned is the same amount for both sales people.  

Can you help me understand where I went wrong.  

I am trying to get my if-else-if statement to work with the array value, but it seems to calculate based on the last Annual Sales value entered by the user.  

If I'm understanding the array functionality correctly...I shouldn't have to do the if-else-if a second time for it to calculate the total compensation for both ee's in the array(?)

mport java.text.DecimalFormat;
import javax.swing.JOptionPane;
/*
 * // created by Cynthia Hill
 */
 *
   * This program stores in an array the necessary information for two Sales Reps.
   */
  public class totalCompCompare_
  {
     public static void main(String[] args)
     {
      String inputString = null;     // For reading input
      String name;             // Variable - The sales reps name
      final int NUM_SALES_REPS = 2; // Number of employees / sales reps
      double annualSales;            // Annual Sales
      double salary = 50000.00;   // Annual Salary Specified in assignment requirements
      double totalCompensation; //Calculated Variable - Calc will be salary+annualCommission
      double annualCommission = 0;  //Calculated Variable -
      double salesCommissionTarget = 150000.00;//Sales Commission Target Specified, used to determine commission
      double minCommissionSales = 120000.00; //80% of Sales Commission Target, used to determine commission

        // Create an array.
        int[] x = new int[NUM_SALES_REPS];

        // Get each employee's name.
       for (int index = 0; index < NUM_SALES_REPS; index++)
   
       
           //get Sales Rep Names
           name = JOptionPane.showInputDialog("Enter the name for Sales Rep test" +
        (index + 1) + ": ");
       
       
          for (int index = 0; index < NUM_SALES_REPS; index++)
       
              //get Sales Reps annual sales
     inputString =
   JOptionPane.showInputDialog("Enter Annual Sales amount for Rep" +
          (index + 1) +" (no commas please): ");
         
    // Convert the input to a double.
     annualSales = Double.parseDouble(inputString);

        // Display each employee's total compensation.
        JOptionPane.showMessageDialog(null, "Total Compensation for each employee:");
       for (int index = 0; index < NUM_SALES_REPS; index++)
        {
           
            if (annualSales < minCommissionSales)
   {
           annualCommission = 0;
         // double annualCommission = 0;
      }
    //2nd IF = If annual sales is greater than or equal to 120000 (80% of sales target), then 5 % commision is given
   else
   {    
        if
           (annualSales >= salesCommissionTarget)
        {
           annualCommission = annualSales * .065;
        }
       
   //3rd IF = If annual sales is greater than Sales target of 150000or equal to 120000, then 6.5 % commision is given
   else
   {      if
           (annualSales >=minCommissionSales)
        {
           annualCommission = annualSales * .05;
        }
      }
    }
    // Calculate the total Compensation.
    totalCompensation = salary + annualCommission;
   
     JOptionPane.showMessageDialog(null, "Employee #" + (index + 1)
                      + ": $"+ totalCompensation);
Avatar of mccarl
mccarl
Flag of Australia image

The only array that I see in your code is the "x" array which is of type int[]. But then you never actually do anything with "x" so in essence, this code has nothing to do with arrays.

And that is why you are seeing the problem you are seeing, because each time around the first loop, you are just overwriting "name" with the next one. And the same for annual sales. So at the end of each loop, the "name" variable only holds the last name entered, and "annualSales" only holds the last sales value.
ASKER CERTIFIED SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cynthia Hill

ASKER

Good explanation! Thanks
Why no points for my part of the explanation?