Joseph B Howle
Joseph B Howle

Reputation: 17

double[][] cannot be converted to int[][]

Can any one tell me why I am receiving the Java compiler error (double[][] cannot be converted to int[][]). It's way down in my program. Here is the whole thing:

public class Salary {

public double medianPay (int p, int [][] pay)
{
    double median = 0;
    int total = 0;
    int staff = pay[p].length;

    for (int i = 0; i < staff; i++ )
    {
        total = total + pay[p][i];
        median = total / staff;
    }
    return total;
}        

public int totalPay (int p, int[][] pay)
{
  int total = 0;
  int staff = pay[p].length;

  for (int i = 0; i < staff; i++ )
  {
      total = total + pay[p][i];
  }
  return total;
}   

public int totalStaff (int p, int [][] pay)
{
    int staff = pay[p].length;
    return staff;
}

public static void main ( String [] args ) {

    double salaries [][] = {    
        {49920, 50831, 39430, 54697, 41751, 36110, 41928, 48460, 39714, 49271, 51713, 38903},       //Alermit (row 0)
        {45519, 47373, 36824, 51229, 36966, 40332, 53294, 44907, 36050, 51574, 39758, 53847},      //Logway (row 1)
        {54619, 48339, 44260, 44390, 39732, 44073, 53308, 35459, 52448, 38364, 39990, 47373}     //Felter    (row 2)
    };

    System.out.println("Which company would you like salaray statistics for?: ");
    System.out.println("Press 0 - Alhermit");
    System.out.println("Press 1 - Logway");
    System.out.println("Press 2 - Felter");

    int user_input = 3;
    String CorpName = "";

    if (user_input < 3)
    {
        if (user_input == 0)
            CorpName = " Alhermit ";
        else if (user_input == 1)
            CorpName = " Logway ";
        else if (user_input == 2)
            CorpName = " Felter ";

        double median = medianPay(user_input, salaries);
        int total = totalPay(user_input,salaries);
        int staffNumber = totalStaff(user_input,salaries);


        System.out.println("The Average Salary of " + CorpName + "is -" + median);
        System.out.println("The Combined Salries of " + CorpName + "is -" + total);
        System.out.println( CorpName + " Has " + staffNumber + " Employee's");
    }
    else
        System.out.println("Please Try Again");
}   
}

The error I receive in on the word salaries in these 3 lines:

double median = medianPay(user_input, salaries);
int total = totalPay(user_input,salaries);
int staffNumber = totalStaff(user_input,salaries);

Upvotes: 1

Views: 610

Answers (2)

MD. NASHID KAMAL
MD. NASHID KAMAL

Reputation: 93

You're passing a double value, So you must need a double variable to accept that. U can't convert anything here. What you can do, you can type cast to int inside your code.

 public static double medianPay (int p, double[][] salaries)
{
    double median = 0;
    int total = 0;
    int staff = salaries[p].length;

    for (int i = 0; i < staff; i++ )
    {
        total = (int) (total + salaries[p][i]);
        median = total / staff;
    }
    return total;
}

Upvotes: 1

dave
dave

Reputation: 11975

Your salaries variable is of type double [][]. You attempt to pass that into functions that accept parameters of type int [][], eg. medianPay(). This is a type clash; hence your error.

You probably want to convert your functions to accept and use double[][]. Or perhaps, seeing as your salaries are all whole numbers, you could convert salaries to be of type int[][]. If you take the latter option, note that you'll get rounding to the nearest integer value when you divide.

Upvotes: 0

Related Questions