2013-10-01 35 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的代碼,以及:您對您的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); 

(鑑於id參數的find方法是你要尋找的ID)

另外,我建議您重新命名例外,包括後綴爲Exception以遵循Java命名約定 - 因此ItemNotFoundException

需要改變你的循環 - 目前你拋出一個異常,如果第一值沒有正確的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

,並沒有通過當您使用它時需要參數。

嘗試通過這裏需要ARGS

throw new ItemNotFound(id); 

所以,你的代碼變得

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(); 

,當你的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(int id),因此throw new Item()無效。

+0

請再來。我不明白這與這個問題有什麼關係。 – SudoRahul

+0

對不起我的錯誤,我以爲你正在傳遞一個int參數給構造函數:super(String.format(「Item%d was not found。」,id));這是目錄類中存在的。其實,throe新的Item()是無效的,因爲你已經定義了一個顯式的構造函數項(int id)...... Pleas忽略了我以前的答案。 –

+0

編輯您的答案並將其發佈到那裏。不在評論中。 – SudoRahul

0

您提供了一個沒有任何參數的構造函數。

public ItemNotFound() 

您在調用new ItemNotFound(id)這在創建類的實例ItemNotFound願與one parameter of type int構造。所以你需要有一個重載的構造函數

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 
    } 
} 
相關問題