Madeline Owens
Madeline Owens

Reputation: 23

Error code incompatible types: possible lossy conversion from double to int

I am in the process of altering this code from an example from my textbook to be compatible with a double array. There are several places in the code where I am not sure how to fix the error "incompatible types: possible lossy conversion from double to int"

temp = numberList[max];    
numberList[max] = numberList[numberList.length - i - 1];
numberList[numberList.length - i - 1] = temp;

if (numberList[i] > numberList[index] )

j = i;
temp = numberList[i];

Those show the error "incompatible types: possible lossy conversion from double to int."

I am also recieveing errors o the first use of indexOfLargestElement, "cannot find symbol."

package week5;
import java.util.Arrays;
/**
 *
 * @author meowens
 */
public class Week5 {

/**
 * @param args the command line arguments
 */

public static void selectionSort( double [] numberList) {

    double temp; 
    double max;

    for (double i = 0; i < numberList.length - 1; i++)  
    {
        max = indexOfLargestElement( numberList, numberList.length - i);

        temp = numberList[max];
        numberList[max] = numberList[numberList.length - i - 1];
        numberList[numberList.length - i - 1] = temp;
    }
}
private static double indexOfLarggestElement( double [] numberList, double size )
{
    double index = 0;
    for ( double i = 1; i < size; i++)
    {
        if (numberList[i] > numberList[index] )
            index = i;
    }
    return index;
}
public static void insertionSort ( double [] numberList)
{
    int j, temp;

    for ( double i = 1; i < numberList.length; i++ )
    {
        j = i;
        temp = numberList[i];

        while ( j != 0 && numberList[j - 1] > temp )
        {
            numberList[j] = numberList[j - 1];
            j--;
        }

        numberList[j] = temp;
    }
    // TODO code application logic here



    // assigning values
    numberList[0] = 53.5;
    numberList[1] = 60.3;
    numberList[2] = 96.2;
    numberList[3] = 53.3;
    numberList[4] = 56.4;
    numberList[5] = 52.7;
    numberList[6] = 76.4;
    numberList[7] = 77.5;
    numberList[8] = 71.0;
    numberList[9] = 78.2;

    numberList[10] = 65.2;
    numberList[11] = 59.3;
    numberList[12] = 80.5;
    numberList[13] = 92.1;
    numberList[14] = 85.7;
    numberList[15] = 78.7;
    numberList[16] = 66.2;
    numberList[17] = 88.8;
    numberList[18] = 50.2;
    numberList[19] = 73.4;

    }

}

I am sure there are other simple errors I have yet to find because I have not yet had a chance to debug and proofread the code and I am also very new to Java and programming in general.

Thanks for the help!

Upvotes: 2

Views: 1569

Answers (2)

Ousmane D.
Ousmane D.

Reputation: 56393

for ( double i = 1; i < numberList.length; i++ )

you cannot use any other type apart from byte,short,int to index through arrays hence the problem. char values may also be used as index values because they are subjected to unary numeric promotion and become int values however very uncommon to see char values used as an indexer.

Ensure, you change double type variables to int type variables wherever you've used double type variables to index through the array.

Java documentation

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int values.

An attempt to access an array component with a long index value results in a compile-time error.

Upvotes: 1

dana
dana

Reputation: 18105

The general issue here was that you were mixing double and int values in a couple places. Your array was of type double[]. So whenever you accessed an element of it, the index should be of type int and it will access a value of type double. I changed the loop and temp variables as such.

The method named indexOfLarggestElement also had a typo which I changed to be indexOfLargestElement.

package week5;

import java.util.Arrays;

/**
 *
 * @author meowens
 */
public class Week5 {

    /**
    * @param args the command line arguments
    */

    public static void selectionSort( double [] numberList)
    {
        double temp; 
        int max;

        for (int i = 0; i < numberList.length - 1; i++)  
        {
            max = indexOfLargestElement( numberList, numberList.length - i);

            temp = numberList[max];
            numberList[max] = numberList[numberList.length - i - 1];
            numberList[numberList.length - i - 1] = temp;
        }
    }

    private static int indexOfLargestElement( double [] numberList, double size )
    {
        int index = 0;
        for ( int i = 1; i < size; i++)
        {
            if (numberList[i] > numberList[index] )
                index = i;
        }
        return index;
    }

    public static void insertionSort ( double [] numberList)
    {
        int j;
        double temp;

        for ( int i = 1; i < numberList.length; i++ )
        {
            j = i;
            temp = numberList[i];

            while ( j != 0 && numberList[j - 1] > temp )
            {
                numberList[j] = numberList[j - 1];
                j--;
            }

            numberList[j] = temp;
        }
        // TODO code application logic here

        // assigning values
        numberList[0] = 53.5;
        numberList[1] = 60.3;
        numberList[2] = 96.2;
        numberList[3] = 53.3;
        numberList[4] = 56.4;
        numberList[5] = 52.7;
        numberList[6] = 76.4;
        numberList[7] = 77.5;
        numberList[8] = 71.0;
        numberList[9] = 78.2;

        numberList[10] = 65.2;
        numberList[11] = 59.3;
        numberList[12] = 80.5;
        numberList[13] = 92.1;
        numberList[14] = 85.7;
        numberList[15] = 78.7;
        numberList[16] = 66.2;
        numberList[17] = 88.8;
        numberList[18] = 50.2;
        numberList[19] = 73.4;
    }
}

Upvotes: 0

Related Questions