类中的构造方法不能应用于给定的类型.希望得到帮助 [英] Constructor in class cannot be applied to given types. Hope for assistance

查看:170
本文介绍了类中的构造方法不能应用于给定的类型.希望得到帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java相当陌生,正在使用BlueJ.我不断收到错误消息:

I'm fairly new to Java and I'm using BlueJ. I keep getting the error:

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.

这是我的班级目录:

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的代码:

For reference, here is the code for the class ItemNotFound as well:

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

推荐答案

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

The ItemNotFound class only has one constructor: one that takes an int parameter:

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

(鉴于find方法的id参数是您要查找的ID.)

(Given that the id parameter to the find method is the ID you're looking for.)

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

Additionally, I'd recommend that you rename your exception to include a suffix of Exception to follow Java naming conventions - so ItemNotFoundException.

您还将 需要更改循环-当前,如果 first 值没有正确的ID,则可能引发异常,而您可能想要遍历所有这些.因此,您的find方法应如下所示:

You'll also need to change your loop - currently you're throwing an exception if the first value doesn't have the right ID, whereas presumably you want to loop through all of them. So your find method should look like this:

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

这篇关于类中的构造方法不能应用于给定的类型.希望得到帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆