Java 类中的构造函数不能应用于给定类型。希望得到帮助
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19109545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Constructor in class cannot be applied to given types. Hope for assistance
提问by BBladem83
I'm fairly new to Java
and I'm using BlueJ
. I keep getting the error:
我是新手Java
,我正在使用BlueJ
. 我不断收到错误:
constructor ItemNotFound in class ItemNotFound cannot be applied to given types;
required: int
found: no arguments
reason: actual and formal arguments lists differ in length
I'm fairly confused and in turn not sure how to fix the problem. Hopefully someone can help me. Thank you in advance.
我很困惑,反过来不知道如何解决这个问题。希望有人可以帮助我。先感谢您。
Here is my class Catalog:
这是我的班级目录:
public class Catalog {
private Item[] list;
private int size;
// Construct an empty catalog with the specified capacity.
public Catalog(int max) {
list = new Item[max];
size = 0;
}
// Insert a new item into the catalog.
// Throw a CatalogFull exception if the catalog is full.
public void insert(Item obj) throws CatalogFull {
if (list.length == size) {
throw new CatalogFull();
}
list[size] = obj;
++size;
}
// Search the catalog for the item whose item number
// is the parameter id. Return the matching object
// if the search succeeds. Throw an ItemNotFound
// exception if the search fails.
public Item find(int id) throws ItemNotFound {
for (int pos = 0; pos < size; ++pos){
if (id == list[pos].getItemNumber()){
return list[pos];
}
else {
throw new ItemNotFound(); //"new ItemNotFound" is the error
}
}
}
}
For reference, here is the code for the class ItemNotFound
as well:
作为参考,这里是代码class ItemNotFound
:
// This exception is thrown when searching for an item
// that is not in the catalog.
public class ItemNotFound extends Exception {
public ItemNotFound(int id) {
super(String.format("Item %d was not found.", id));
}
}
采纳答案by Jon Skeet
The ItemNotFound
class only has one constructor: one that takes an int
parameter:
本ItemNotFound
类只有一个构造函数:一个接受一个int
参数:
public ItemNotFound(int id)
You're trying to call that without any arguments:
你试图在没有任何参数的情况下调用它:
throw new ItemNotFound();
That's not going to work - you need to pass an argument for that parameter. I suspect you just want:
那是行不通的 - 您需要为该参数传递一个参数。我怀疑你只是想要:
throw new ItemNotFound(id);
(Given that the id
parameter to the find
method is the ID you're looking for.)
(假设id
该find
方法的参数是您要查找的 ID。)
Additionally, I'd recommend that you rename your exception to include a suffix of Exception
to follow Java naming conventions - so ItemNotFoundException
.
此外,我建议您将异常重命名为包含后缀Exception
以遵循 Java 命名约定 - 所以ItemNotFoundException
.
You'll alsoneed to change your loop - currently you're throwing an exception if the firstvalue doesn't have the right ID, whereas presumably you want to loop through all of them. So your find
method should look like this:
您还需要更改循环 - 目前,如果第一个值没有正确的 ID,您将抛出异常,而大概您想遍历所有这些。所以你的find
方法应该是这样的:
public Item find(int id) throws ItemNotFoundException {
for (int pos = 0; pos < size; ++pos){
if (id == list[pos].getItemNumber()){
return list[pos];
}
}
throw new ItemNotFoundException(id);
}
回答by Suresh Atta
You provided a custom constructor to your class ItemNotFound
and not passing the required arguments when you are using it.
您为您的类提供了一个自定义构造函数, ItemNotFound
并且在使用它时没有传递所需的参数。
Try to pass required args here
尝试在此处传递所需的参数
throw new ItemNotFound(id);
So your code becomes
所以你的代码变成
public Item find(int id) throws ItemNotFound {
for (int pos = 0; pos < size; ++pos){
if (id == list[pos].getItemNumber()){
return list[pos];
}
else {
throw new ItemNotFound(id); // Now constructor satisfied
}
}
}
And
和
throw new ItemNotFound();
That above line is true when your class ItemNotFound
is like
当您的课程 ItemNotFound
类似于
// This exception is thrown when searching for an item
// that is not in the catalog.
public class ItemNotFound extends Exception {
public ItemNotFound() {
super("Sorry !! No item find with that id"); //Now a generic message.
}
}
回答by Vineet Kasat
throw new Item() is not valid as an explicit constructor has been defined Item(int id).
throw new Item() 无效,因为已定义了显式构造函数 Item(int id)。
回答by Aniket Thakur
You have provided a constructor without any parameters.
您提供了一个没有任何参数的构造函数。
public ItemNotFound()
and you are calling new ItemNotFound(id)
which while creating instance of class ItemNotFound expects a constructor with one parameter of type int
. So you need to have an overloaded constructor
并且您new ItemNotFound(id)
在创建类 ItemNotFound 的实例时调用which 需要一个带有one parameter of type int
. 所以你需要有一个重载的构造函数
public class ItemNotFound extends Exception {
public ItemNotFound() {
super("Sorry !! No item find with that id"); //Now a generic message.
}
public ItemNotFound(int if) {
this(); //Since you are not using any int id in this class
}
}