0

I have this code written:

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    Liber l = new Liber();// and here, the second 'Liber', it's telling me to "create constructor 'Liber()' in tipetnumerike.Liber"

Whilst the code to my other class is like this:

package tipetnumerike;

public class Liber {

private int id;
private String Titulli;
private int VitiPublikimit;

public Liber(int i, String T, int VP) {
    id = i;
    VitiPublikimit = VP;
    T = Titulli;
}

public int getId() {
    return id;

}

public String getTitulli() {
    return Titulli;
}

public int getVitiPublikimit() {
    return VitiPublikimit;
}

public void setVitiPublikimit(int VP) {
    VitiPublikimit = VP;
}

public void setTitulli(String T) {
    Titulli = T;
}

public String ToString() {
    return id + ':' + Titulli + '-' + VitiPublikimit;

}

public boolean equals(Object o) {
    if (o instanceof Libri) {

        return id == ((Liber) o).getId();

    }
    return false;
}

}

I don't know what I did wrong, I don't think I should be throwing a constructor, I want to get the Liber class, in my other projects, this didn't happen.

alison
  • 13
  • 3
  • 2
    Because your class doesn't have a default constructor. It only has one that takes an int, String, int – Thiyagu May 08 '18 at 16:04
  • What is the exact text of the error message you get? Did you import the package name `tipetnumerike`? – markspace May 08 '18 at 16:04

2 Answers2

0

You don’t have a constructor for Liber without any arguments, but you are trying to construct it like that.

Either pass your required int i, String T, int VP (note you may want to look at naming conventions) or, like the message says, create an empty constructor in your Liber class. The latter is probably not what you want since then you need to obtain those values you need in another way later. You have setters for them too but that just makes your code more verbose for no reason.

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73
0

You use new Liber(); which means to create an instance without parameter, by default this constructor exists BUT because you create another constructor Liber(int i, String T, int VP) it does not exist now, you have to add it :

public Liber(){ ... }

Also

  • follow Java naming conventions :packages, attributes, variables, parameters, method have to start in lowerCase
  • ToString() -> toString() if not it won't be used
azro
  • 53,056
  • 7
  • 34
  • 70