【问题标题】:Method cannot be applied to given types. Hope for assistance方法不能应用于给定类型。希望得到帮助
【发布时间】:2013-10-07 05:57:59
【问题描述】:

我对@9​​87654322@ 还很陌生,我正在使用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();
        }
    }
}

作为参考,这里也是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];
            }
        }
        throw new ItemNotFound(id);
        }
}

【问题讨论】:

标签: java methods bluej


【解决方案1】:

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

   public Item find(int id) throws ItemNotFound {

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

itemnum = store.find(item); 

你只需要反转它

 item= store.find(itemnum); 

意思是

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

【讨论】:

    【解决方案2】:

    itemnum = store.find(item) 更改为item = store.find(itemnum)

    【讨论】:

      猜你喜欢
      • 2013-10-07
      • 2013-10-09
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      • 2013-07-27
      • 2012-10-21
      相关资源
      最近更新 更多