Pro-grammer
Pro-grammer

Reputation: 25

Double array method error: Incompatible types: possible lossy conversion from double to int

I'm trying to input a double array but I'm getting an error message in my code

public static double getHighNumber(double[] numbers) {
    int a, b, t;

    for (a = 2; a < 11; a++) {
        for (b = 10; b + 1 >= a; b--) {
            if (numbers[b - 1] > numbers[b]) {
                t = numbers[b - 1];
                numbers[b - 1] = numbers[b];
                numbers[b] = t;
            }
        }
    }
    return numbers[10];
}

on the line t = numbers[b-1];

It compiles and works properly if I use int arrays but not double arrays. How can I change the method so it accepts double arrays?

Upvotes: 3

Views: 223

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201399

numbers is a double, t should be too (based on your code, that will make t = numbers[b-1]; valid). Something like,

int a, b; // , t;
double t;

but, if you just want the highest number; then you could do something like

public static double getHighNumber(double[] numbers) {
    double h = numbers[0];
    for (int i = 1; i < numbers.length; i++) {
        h = Math.max(h, numbers[i]);
    }
    return h;
}

Upvotes: 2

Related Questions