2

我很新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

我很困惑,反过来不知道如何解决这个问题。希望有人可以帮助我。先感谢您。

这是我的班级目录:

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

作为参考,这里也是代码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));
    }
}
4

4 回答 4

3

该类ItemNotFound只有一个构造函数:一个带int参数的构造函数:

public ItemNotFound(int id)

您试图在没有任何参数的情况下调用它:

throw new ItemNotFound();

那是行不通的——你需要为那个参数传递一个参数。我怀疑你只是想要:

throw new ItemNotFound(id);

(假设该方法的id参数find是您要查找的 ID。)

此外,我建议您重命名异常以包含后缀Exception以遵循 Java 命名约定 - so ItemNotFoundException

还需要更改循环 - 如果第一个值没有正确的 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);
}
于 2013-10-01T06:39:23.480 回答
2

您为您的类提供了一个自定义构造函数, ItemNotFound 并且在使用它时没有传递所需的参数。

尝试在此处传递所需的参数

  throw new ItemNotFound(id); 

所以你的代码变成

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

throw new ItemNotFound();

当您的班级 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.
    }
}
于 2013-10-01T06:38:13.503 回答
0

throw new Item() 无效,因为已定义了显式构造函数 Item(int id)。

于 2013-10-01T06:38:19.117 回答
0

您提供了一个没有任何参数的构造函数。

public ItemNotFound()

并且您正在调用new ItemNotFound(id)which 在创建类 ItemNotFound 的实例时需要一个带有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
    }
} 
于 2013-10-01T06:55:30.920 回答