2013-10-01 2 views
2

나는 꽤 Java에 새로 출연했고 나는 BlueJ를 사용하고있다. 계속 오류가 발생합니다.클래스의 생성자를 특정 유형에 적용 할 수 없습니다. 도움을 청하십시오.

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 

나는 상당히 혼란스럽고 문제를 해결하는 방법을 모릅니다. 잘만되면 누군가 나를 도울 수 있습니다. 미리 감사드립니다.

참고로
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 코드입니다 :

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

답변

3

ItemNotFound 클래스는 하나의 생성자가 있습니다 int 매개 변수를 하나 : 당신은 전화를 위해 노력하고

public ItemNotFound(int id) 

을 그 인수없이 : 작동하지 않을 것

throw new ItemNotFound(); 

- 해당 매개 변수에 대한 인수를 전달해야합니다. 난 그냥 원하는 의심 :

throw new ItemNotFound(id); 

(. find 방법에 id 매개 변수는 당신이 찾고있는 ID는 점을 감안)

또한, 당신이 포함하도록 예외의 이름을 변경하는 것이 좋습니다 것 자바 명명 규칙을 따르는 접미사 Exception - 그래서 ItemNotFoundException.

당신거야 또한 루프를 변경해야 - 현재 당신이 예외를 throw하고있는 값이 올바른 ID가없는 경우, 아마 당신은 그들 모두를 통해 루프를 원하는 반면. 따라서 find 메소드는 다음과 같아야합니다.

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

당신은 당신의 클래스 ItemNotFound에 사용자 정의 생성자를 제공가와를 통과하지 여기

내 클래스 카탈로그입니다 당신이 그것을 사용할 때 필요한 인수. 여기

throw new ItemNotFound(id); 

필요한 인수를 전달하는

시도는 그래서 코드는 클래스 ItemNotFound

같은 경우 위의 라인은 사실이다

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(id); // Now constructor satisfied 
      } 
     } 
    } 

그리고

throw new ItemNotFound(); 

된다

// This exception is thrown when searching for an item 
// that is not in the catalog. 
public class ItemNotFound extends Exception { 
    public ItemNotFound() { 
     super("Sorry !! No item find with that id"); //Now a generic message. 
    } 
} 
0

새로운 Item()이 명시 적 생성자로 유효하지 않음 (Item (int id))이 정의되었습니다.

+0

다시 방문하십시오. 나는이 질문과 어떻게 관련이 있는지 이해하지 못했다. – SudoRahul

+0

죄송합니다. 실수로 생성자에 int 인수를 전달한다고 생각했습니다. super (String.format ("Item % d을 (를) 찾을 수 없습니다.", id)); 카탈로그 클래스에 있습니다. 사실, 명시 적 생성자 항목 (int id)을 정의 했으므로 new Item()이 유효하지 않습니다. 이전 답변을 무시합니다. –

+0

답변을 수정하여 게시하십시오. 여기 코멘트가 아닙니다. – SudoRahul

0

매개 변수없이 생성자를 제공했습니다.

public ItemNotFound() 

당신은 클래스의 인스턴스를 만들 때 ItemNotFound이 one parameter of type int와 생성자를 기대 new ItemNotFound(id)을 요구하고있다. 따라서 오버로드 된 생성자가 필요합니다.

public class ItemNotFound extends Exception { 
    public ItemNotFound() { 
     super("Sorry !! No item find with that id"); //Now a generic message. 
    } 

    public ItemNotFound(int if) { 
     this(); //Since you are not using any int id in this class 
    } 
}