简体   繁体   中英

Having troubles of passing an object with multiple variable types into arraylist in java

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.

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 .

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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM