TimC
TimC

Reputation: 13

incompatible types - double to int?

Java seems to think that I am attempting to convert or perform some kind of action on one of my double variables. I get the error message

average2.java:23: error: incompatible types: possible lossy conversion from double to int scores[count++]=score;

I am really confused in that I have not declared anything as an integer thus far, - every variable is a double because I expect to have some decimals. Below is my code :

public static void main (String [] args)
{

    double numOf;
    double lowest = 100;
    double count = 0;
    double sum = 0;

    double average = 0;
    double score;

    double scores[] = new double[100]; //[IO.readDouble("Enter Scores, Enter -1 to Quit")];

    while ((count <100) &&(( score =IO.readDouble("Enter Scores,  (-1 to Quit")) > 0));
    {
        scores[count++]=score;
    }

    //This section obtains the highest number that was entered`
    double max = scores[0];
    for (double i=0; i<scores.length; i++)
        if(max < scores[i])max =scores[i];

    System.out.println("Maximum is " + max);    

    // This section obtains the lowest score entered    
    double min = scores[0];
    for (int i=0; i<scores.length; i++)
        if (min > scores[i]) min = scores [i];

    int sumOf =0;
    for (int i=0; i < scores.length; i++)
    {    
        sumOf += scores[i];     
    }
    System.out.println("The sum of all scores is  " + sumOf);

    System.out.println("Minimum is " + min);

    count = count + 1;                     

    average =  (sumOf/scores.length);     
    System.out.println("Average is " + average);
} //end main      
} //end class     

Upvotes: 0

Views: 1152

Answers (1)

rgettman
rgettman

Reputation: 178263

The error refers to the count variable, which is a double. But ints are valid indices for an array. The error results from using a double as an index where an int was expected for the index.

Declare count to be an int.

You should also declare i to be an int in the first for loop, for the same reason.

Upvotes: 5

Related Questions