2013-10-01 3 views
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 

매우 혼란스럽고 문제를 해결하는 방법이 확실하지 않습니다. 잘만되면 누군가 나를 도울 수 있습니다. 미리 감사드립니다. 여기, 참고로

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입니다 : 여기

내 program2에 클래스입니다

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

'Item'을'int'로 넣으려고합니다. 그래서'Item' 정의는 어디에 있습니까? 'store.find (item.getId())'또는 'store.find (item.id)'와 같은 것이어야합니다. –

+0

가능한 [클래스의 생성자는 주어진 유형에 적용 할 수 없습니다. 도움을위한 희망] (http://stackoverflow.com/questions/19109545/constructor-in-class-cannot-be-applied-to-given-types-hope-for-assistance) –

+0

@SubirKumarSao 약간 반대. 둘 다 다른 질문입니다 :) –

답변

6

여기에 귀하의 find() 메소드 서명

public Item find(int id) throws ItemNotFound { 

이 인수로 INT를 수신 . 그러나 당신은 전달하고있다 Item obj

itemnum = store.find(item); 

요법

당신은 그냥

item= store.find(itemnum); 

당신의 발견 방법은 Item 객체를 반환 이후

item는, 아이디 itemnum으로 찾아 할당을 의미 반대해야합니다.

0

변경 itemnum = store.find(item)에서 item = store.find(itemnum)으로 변경하십시오.