0

I am trying to save an object, that is created from a file, into an arraylist and I am having troubles with doing exactly that in a three-class program. I'm getting an error telling me to create constructors, but when I auto create them I get an error saying: Constructor cannot be applied to given types. I am not sure what it means. In short - I've been at this thing for hours now and cannot figure it out.

public class Darbuotojas {
    String vardas;
    String pareigos;
    int gm;
    Float atlyginimas;

    public Darbuotojas(String vardas, String pareigos, int gm, Float atlyginimas){
        this.vardas = vardas;
        this.pareigos = pareigos;
        this.gm = gm;
        this.atlyginimas = atlyginimas;
    }
}

Here is the code where I read the file, and try to put Objects Darbuotojas into an ArrayList:

public class Viskas extends Darbuotojas{
    String gm1;
    String atlyginimas1;
    ArrayList<Darbuotojas> darbuotojai = new ArrayList<Darbuotojas>();

    public void failas(String fl) throws IOException{
        //Failu nuskaitymas po zodi
        File file = new File(fl);
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line = null;

            while ((line = br.readLine()) != null){
                String [] tokens = line.split("/");
                vardas = tokens[0];
                pareigos = tokens[1];
                gm1 = tokens[2];
                gm = Integer.parseInt(gm1);
                atlyginimas1 = tokens[3];
                atlyginimas = Float.parseFloat(atlyginimas1);
                System.out.print(vardas.toUpperCase() + " ");
                System.out.print(pareigos.toUpperCase() + " ");
                System.out.print(gm + " ");
                System.out.println(atlyginimas);  
                Darbuotojas drb = new Darbuotojas(vardas,pareigos,gm,atlyginimas);
                darbuotojai.add(drb);
                System.out.println(drb);
            }

            br.close();
        }
        catch(FileNotFoundException e){
        }
    }
}

And here is the main function:

public static void main(String[] args) throws IOException {
    Scanner kb = new Scanner(System.in);
    System.out.println("Iveskite duomenu vailo pavadinima su failo tipu: ");
    String fl = kb.next();

    Viskas ddd = new Viskas();

    ddd.failas(fl);
}

I'm sorry about all the variables being in Lithuanian, but I don't think it changes anything code readability wise.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • Within your `while` loop, how did you declare the `vardas` and `pareigos` variables? (in `vardas = tokens[0]` and `pareigos = tokens[1]`. And can you post the full error message so that we can see what is wrong with the constructor? – Al-un Oct 07 '18 at 20:49
  • Thank you for your answer, @AL-un, I did not declare them inside the loop as I have them delcared in the 'Darbuotojas' class. the full message reads: " construcotr Darbuotojas in class Darbuotojas cannot be applied to given types; required: String, String, int, Float; found: no arguments;reason:actual and formal argument lists differ in length." – Dominykas Stonys Oct 07 '18 at 20:56

3 Answers3

0

In your example, you are instantiating a new Viskas. Since Viskas extends Darbuotojas and does not have its own constructor, the constructor for Darbuotojas is called with missing parameters. To resolve this, either pass in the required parameters when making a new Viskas:

(String vardas, String pareigos, int gm, Float atlyginimas)

or create a parameterless constructor for Darbuotojas.

Denis
  • 25
  • 8
  • 1
    Hi, @Denis, thank you for your answer. When I create parameterless constructor for Viskas the code wont compile and I get the error : "construcotr Darbuotojas in class Darbuotojas cannot be applied to given types; required: String, String, int, Float; found: no arguments;reason:actual and formal argument lists differ in length." – Dominykas Stonys Oct 07 '18 at 21:06
  • My original comment had a typo; I meant create a parameterless constructor for `Darbuotojas`. Edited. – Denis Oct 07 '18 at 21:11
0

Darbuotojas has a defined constructor. Viskas extends Darbuotojas, yet it does not invoke the constructor of its parent Darbuotojas. Just create a constructor for Viskas and put super() at the top.

DanielGuy
  • 70
  • 3
  • Hi, @Daniel, thank you for the comment. I did what you and @Denis suggested and it seems to compile now. Thank you, guys. But now im trying to printout the ArrayList with `System.out.println(Arrays.toString(darbuotojai.toArray()));` and seems like it all went wrong somewhere, all it prints out is "kontras.Darbuotojas@55f96302" for each item on the list. – Dominykas Stonys Oct 07 '18 at 21:31
  • That is the expected behavior because you are trying to print an object. If you need to get some information when you print the object try to implement toString() method inside Darbuotojas class. https://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – Rans Oct 07 '18 at 22:21
0

Maybe an example helps to explain the solution better. Let's assume you have two classes called Vehicle and Car. Car extends Vehicle so that Car can inherit all the features from Vehicle.

Parent Class:

public class Vehicle{
    String name;
    int age;
}

Child Class:

public Class Car extends Vehicle {
 String type;
}

When you don't have constructors defined in the classes it will automatically create those inside and bellow is how it will look like.

Parent Class:

public class Vehicle{

    String name;
    int age;

    public Vehicle(){
        super();
    }
}

Child Class:

public Class Car extends Vehicle {
    String type;

    public Car(){
        Super();
    }
}

When you have the constructor defined inside the parent class like bellow it will no longer have default constructors with empty parameters.

public class Vehicle{

    String name;
    int age;

    public Vehicle(String name, int age){
        this.name = name;
        this.age = age;
    }
}

So if you try to create a child class without defining constructor it will look like below.

public Class Car extends Vehicle {
    String type;

    public Car(){
        Super();
    }
}

Now above will give a compilation error because the child class (Car) is trying to call its parent class(Vehicle) constructor with empty parameters. So to resolve this issue you have two options.

Option 1: Add empty parameter constructor to the parent class.

public class Vehicle{

    String name;
    int age;

    public Vehicle(){
    }

    public Vehicle(String name, int age){
        this.name = name;
        this.age = age;
    }   
}

Option 2: Define a constructor in child class which calls the parent class constructor.

public Class Car extends Vehicle {
    String type;

    public Car(){
        String defaultName = "";
        int defaultAge = 0;
        Super(defaultName, defaultAge);
    }
}
Rans
  • 422
  • 2
  • 8