2

私はかなり新しく、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();
        }
    }
}

参考までに、以下に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);
        }
}
4

2 に答える 2

6

ここにあなたのfind()メソッド署名

   public Item find(int id) throws ItemNotFound {

Item引数としてintを受け取りますが、オブジェクトを渡しています

itemnum = store.find(item); 

逆にすればいいだけ

 item= store.find(itemnum); 

つまり

itemitemnumfind メソッドがオブジェクトを返すため 、 id で検索することによって割り当てられ Itemます。

于 2013-10-01T07:11:26.843 に答える
0

に変更itemnum = store.find(item)item = store.find(itemnum)ます。

于 2013-10-01T07:12:53.737 に答える