Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

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);
0
Cynthia Hill
Asked:
Cynthia Hill
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0
 
Amitkumar PSr. ConsultantCommented:
Agree with mccarl.

You should have an array for each input elements (like name, annualSales etc.)

I've modified the code (for annualSales only).


import 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 = new double[NUM_SALES_REPS]; // 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[index] = 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[index] < 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[index] >= salesCommissionTarget)
                {
                    annualCommission = annualSales[index] * .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[index] >=minCommissionSales) {
                        annualCommission = annualSales[index] * .05;
                    }
                }
            }
            // Calculate the total Compensation.
            totalCompensation = salary + annualCommission;

            JOptionPane.showMessageDialog(null, "Employee #" + (index + 1)
                    + ": $"+ totalCompensation); 
        }
    }
}

Open in new window

0
 
Cynthia HillLead ConsultantAuthor Commented:
Good explanation! Thanks
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Why no points for my part of the explanation?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now