Reputation: 1
I am writing a CSV parser, im almost done but i have an issuse with a return method. The code is almost the same as from this site, but I still get error.
private static Book createBook(String[] metadata) {
String name = metadata[0];
int price = Integer.parseInt(metadata[1]);
String author = metadata[2];
//Create and return book of this metadata
return new Book(name, price, author);
}
I get an error:
Error:(128, 16) java: constructor Book in class java.awt.print.Book cannot be applied to given types;
required: no arguments
found: java.lang.String,int,java.lang.String
reason: actual and formal argument lists differ in length
I am not sure what is causing this error, ive been dealing with this for many hours now. Thanks in advance.
Upvotes: 0
Views: 1249
Reputation: 719189
Evidence:
The compilation error message talks about java.awt.print.Book
Your code is trying to create a Book
like this: new Book(name, price, author)
Analysis: Those parameters make no sense for an java.awt.print.Book
instance. The latter's javadoc says:
The
Book
class provides a representation of a document in which pages may have different page formats and page painters. This class uses thePageable
interface to interact with aPrinterJob
.
So, it looks like you have accidentally imported java.awt.print.Book
into your class when you really meant to import / use your own Book
class. It probably was a result of an inappropriate "auto-correction" hint from your IDE.
Solution: Delete / replace the bogus import statement.
Upvotes: 2
Reputation: 1772
The concrete cause for the error is, that the Book
class from the java.awt.print
package does only have a zero-argument constructor and therefore can only be instantiated like this:
Book b = new Book();
And here comes something into play which can happen from time to time. The JDK API often feaatures classes which might sound like we could use them for our projects but often their name is a bit misleading or the desired functionality is not available.
If that's the case you should create/use your own class.
Assuming you already have a book class similar to this one:
public class Book
{
private final String name;
private final int price;
private final String author;
public Book( String name, int price, String author )
{
this.name = name;
this.price = price;
this.author = author;
}
// Getter....
}
You have to import the correct one for your project.
You can do so by adding a correct import statement:
import all.my.packages.Book
Another issue you may run into is if you want to use your own version of Book
but also the one from the java.awt.print
package. Now we run into a name clash and you have to specify each object when used:
public static void main( String[] args )
{
String author = "SomeAuthor";
String name = "SomeBook";
int price = 30;
java.awt.print.Book awtBook = new java.awt.print.Book();
playground.test.main.Book myBook = new playground.test.main.Book( name, price, author );
}
But that's something you want to avoid most of the time.
Upvotes: 0