Error: incompatible type in constructor, string can not be converted to int

1

The solution to my problem must be a dull one but I can not see it. I am creating constructors with different variables and constants and one of them has two variables and two constants. This throws me a compatibility error that I do not understand why it happens. Thanks for the help.

package recursos;


public class Electrodomestico {
    private int precioBase;
    private String color;
    private char consumoEnergetico;
    private int peso;

    private static final int PRECIOBASE=100;
    private static final String COLOR="blanco";
    private static final char CONSUMO='F';
    private static final int PESO=5;

    public Electrodomestico(){
        this(PRECIOBASE, COLOR, CONSUMO, PESO);

    }

here the error happens.

    public Electrodomestico(int precioBase, int peso) {
        this(COLOR, CONSUMO);
        this.precioBase = precioBase;
        this.peso = peso;

    }


    public Electrodomestico(int precioBase, String color, char consumoEnergetico, int peso) {
        this.precioBase = precioBase;
        this.color = color;
        this.consumoEnergetico = consumoEnergetico;
        this.peso = peso;
    }

}
    
asked by carpopper 05.12.2018 в 13:48
source

2 answers

2

Let's analyze your constructors and you will see the problem:

The "base" constructor is this:

public Electrodomestico(int precioBase, String color, char consumoEnergetico, int peso) {
    this.precioBase = precioBase;
    this.color = color;
    this.consumoEnergetico = consumoEnergetico;
    this.peso = peso;
}

Receive 4 parameters and save them, correct

Then you have a constructor without parameters:

public Electrodomestico(){
    this(PRECIOBASE, COLOR, CONSUMO, PESO);
}

This delegate to the previous one, passing it 4 values that will be the default values.

Finally you have:

public Electrodomestico(int precioBase, int peso) {
    this(COLOR, CONSUMO); //???
    this.precioBase = precioBase;
    this.peso = peso;

}

This constructor tries to delegate to another that receives two parameters: color (String) and weight (int), but you do not have any constructor that matches that signature, so the compiler assumes that it is a recursive call: this is the only constructor with two parameters, but they do not match the type and therefore it fails.

I assume that what you wanted to do was

public Electrodomestico(int precioBase, int peso) {
    this(precioBase, COLOR, CONSUMO, peso);
}
    
answered by 05.12.2018 в 13:59
0

COLOR is of type String and CONSUMO of type char but you have not defined any constructor that receives parameters of those types.

Or you define a constructor that receives the types you want:

public Electrodomestico(String color, Char ch)
{
 ///
}

Or send the correct parameters to one of the constructors that you already have defined.

    
answered by 05.12.2018 в 14:00