java - 方法不能应用于给定类型。希望得到帮助

标签 java methods bluej

我是 Java 的新手,我正在使用 BlueJ。我不断收到错误消息:

method find in class Catalog cannot be applied to given types;
required: int
found: Item
reason: actual argument Item cannot be converted to int by method invocation conversion

我很困惑,也不知道如何解决这个问题。希望有人能帮助我。提前谢谢你。

这是我的 Program2 类:

import java.util.*;

public class Program2 {
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        Catalog store = new Catalog(3);
        int itemnum;
        Item item;

        try {
            store.insert
              (new Music(1111, "Gold", 12.00, "Abba"));
            store.insert
              (new Movie(2222, "Mamma Mia", 16.00, "Meryl Streep"));
            store.insert
              (new Book(3333, "DaVinci Code", 8.00, "Dan Brown"));
              store.insert
            (new Music(4444, "Legend", 15.00, "Bob Marley"));
            } catch (CatalogFull exc) {
                System.out.println(exc);
            }

        //  Insert code here to perform a sequence of
        //  interactive transactions with the user.
        //  The user enters an item number and the program
        //  either displays the item or prints an error message
        //  if the item is not found.  The program terminates
        //  when the user enters zero as the item number.

        while (!item.equals("0")) {
            itemnum = store.find(item); //Getting error on ".find"
            if (itemnum != null) {
                System.out.print(itemnum);
            } else {
                System.out.printf("%s was not found.%n", item);
            }
            System.out.println();
            System.out.print("Player (0 to exit)? ");
            item = kbd.next();
        }
    }
}

作为引用,这里还有类目录:

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];
            }
        }
        throw new ItemNotFound(id);
        }
}

最佳答案

这里是你的find()方法签名

   public Item find(int id) throws ItemNotFound {

接收 int 作为参数。但是您传递的是 Item 对象

itemnum = store.find(item); 

你只需要扭转它

 item= store.find(itemnum); 

意思是

item 是通过查找 ID itemnum 来分配的,因为您的查找方法返回 Item 对象。

关于java - 方法不能应用于给定类型。希望得到帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110014/

相关文章:

用于 GUI 开发的 Java IDE

java - 如何从另一个类调用方法函数?

java - 2D 数组列表错误

java - 这两种方法有什么区别? JAVA

java - 为什么 JNI Activity 实例会阻止 GC?

java - 尝试从 Array.asList 返回的列表中删除时出现 UnsupportedOperationException

java - Android访问IO

java - 使用同步方法的类的多个实例

Java问题,如何从未知对象中获取方法的值

java.sql.SQLException : ORA-00942: table or view does not exist, 每三次命中数据库一次