Hi, I am having trouble having the answer increment, I know it seems the increment value is lost in the method but I cannot find a way to make that work, can you please give some suggestions. Thanks. This program is supposed to ask the user to enter sales amount and increment the value corresponding to the weekly salary.

public class SalesCommissions {
//metthos to get input from the user
 private int array[];



    public void SalesCalc()
 {
        Scanner input = new Scanner(System.in);
      //create an array to hold the values of the salaries earned per week
        
         int array[] = new int [ 9 ];
        //set the array values to 0
    
   for ( int counter = 0; counter < array.length; counter++ )
         array[ counter ] = 0;
      //ask the user to enter the grross sales amount per week

      System.out.print("Please enter the sales amount(-1 to exit)");
      double saleAmount = input.nextDouble();
     //calculate the commission and weekly salary

      while(saleAmount >= 0) {
        
         // calculate commission
         double commission = 0.09*saleAmount;
          // calculate weekly salary based on commission
         double salary = commission+200;
         
      
       System.out.println( "Enter sales amount (-1 to exit): " );
       saleAmount = input.nextDouble();
       }}// end while
   //   displayResults();

      private void displayResults(double salary, int array[]){

    array = new int[9];
     int[] frequency= new int[9];

       if (salary>= 1000.00) {

              ++frequency[array[0]++];

        } else if (salary>= 900.00) {

              ++frequency[array[0]++];

        } else if (salary >= 800.00) {

             ++frequency[array[0]++];

        } else if (salary >= 700.00) {

              ++frequency[array[0]++];

        } else if (salary >= 600.00) {

              ++frequency[array[0]++];

        } else if (salary >= 500.00){

              ++frequency[array[0]++];

        } else if (salary >= 400.00) {

             ++frequency[array[0]++];

        } else if (salary >= 300.00) {

            ++frequency[array[0]++];

        } else if (salary >= 200.00) {

          ++frequency[array[0]++];
         }
   //end method increment


       for ( int i= 0; i < array.length; i++ )
         array[ i ] = 0;
     for ( int i= 0; i< array.length; i++ )
     ++frequency[array[i]];

     System.out.println("Values\t\tSalary");
     for ( int j= 0; j< frequency.length; j++ )
     System.out.printf( "$%d-$%d     \t%d\n",(200 + 100 * j), (299 + 100 * j), frequency[j]);

      
     //   displayResults(saleAmount, array);
    }//end method Sales Calc

     
      //method to increment array
     
    
      //method to display the results
    
}

There are a few issues with this code.


- The series of if statements starting at

int[] frequency= new int[9];

       if (salary>= 1000.00) {

         ...

is equivalent to

if (salary>= 1000.00) {

              ++frequency[array[0]++];

        }

-Since you're dealing with newly-declared (and unassigned) arrays, the value of each element is zero, so

++frequency[array[0]++];

is always equivalent to

frquency[1]=1;

-frequency and array are local variables. They only exist during the method call. If you want to use those values you have to return them or assign them to a global. (and maybe you should do something with them other than setting them mostly to 0)

- This comment is incorrect:

//end method increment

since there is no method "increment" and no method ends here. Incorrect comments are a bad sign.

- This loop declaration

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

contains an invariant array.length, which refers to a hard-coded constant. The loop body declares array to zero, which it already is. The following loop declares increments another array which is empty except for element [1], in some cases.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.