BobaJ
BobaJ

Reputation: 17

Compilation error - incompatible types

I am working on a program and I've got the massage:

error: incompatible types: int cannot be converted to String if(Double.parseDouble(obtenerMedallasEqu(nombre)) > promedio())

when I tried to compilate it. To be honest, I've no idea of how to correct this error.

The methods, that are relevant for this part of the code

public int obtenerMedallasEqu(String nombre){
    int i = 0;
    int medallas = 0;
    while (i<cantEquipos){
        if(equipos[i].getNombre().equals(nombre))
            medallas = medallas + equipos[i].getOro() + equipos[i].getPlata() + equipos[i].getBronce();
    i++;
    }
    return medallas;
}
public double promedio(){
    return (olimpiadasant[0] + olimpiadasant[1] + olimpiadasant[2] + olimpiadasant[3] + olimpiadasant[4])/5;
}

public String medallasOlimp(){
    int i = 0;
    String resultado = "";
    String nombre = "";
    while(i < cantEquipos){
        if(Double.parseDouble(obtenerMedallasEqu(nombre)) > promedio())
            resultado = "La cantidad de medallas obtenidas en Londres del " + i + " equipo es \n mayor que el promedio de medallas obtenidas en las \n 5 olimpiadas precedentes.";
        i++;
    }
    return resultado;
}

Maybe you guys can help me, or at least give me a tip of how to resolve it. Already thank you.

Upvotes: 1

Views: 232

Answers (1)

Mshnik
Mshnik

Reputation: 7032

Let's look at Double.parseDouble(obtenerMedallasEqu(nombre)). obtenerMedallasEqu(nombre) has a return type of int. However, Double.parseDouble(String s) is expecting a string, not an int. Hence your error. It seems the Double.parseDouble(..) call is unnecessary - it's already an int, you don't need to parse anything to a double. Try changing the line to:

if( ((double) obtenerMedallasEqu(nombre)) > promedio())

Upvotes: 3

Related Questions