2013-01-24 109 views
0

這裏是我的代碼爲我的成份類:找不到構造函數符號

public class Ingredient { 

/* Attribute declarations */ 
private String name; // name 
private int calorieCount; //calorie count 

    /* Constructor */ 
public Ingredient(String name, int calorieCount) { 
    this.name = name; 
    this.calorieCount = calorieCount; 
} 

public String getName(){ 
    return name; 
} 

    public int getCalorieCount(){ 
    return calorieCount; 
} 

    public static void main (String[] args) 
    { 
    Ingredient item1 = new Ingredient ("Butter", "100"); 
    System.out.println(item1); 
    } 
} 

當我嘗試和運行它,我得到一個編譯錯誤:

1 error found: 
File: C:\eclipse\workspace\Assignment NEW1\Ingredient.java [line: 28] 
Error: C:\eclipse\workspace\Assignment NEW1\Ingredient.java:28: cannot find symbol 
symbol : constructor Ingredient(java.lang.String,java.lang.String) 
location: class Ingredient 

我在做什麼錯?

+2

「String」不是「int」「100」是字符串,而「100」是「int」 –

+1

謝謝你的幫助@PeterLawrey ...非常感謝:) –

+0

@MichelleFrazer ..歡迎來到SO。請標記爲已接受的答案。您需要這樣做,只要你得到有用的答案你的問題。 –

回答

4

你傳入100作爲字符串在構造函數: -

Ingredient item1 = new Ingredient ("Butter", "100"); 

將其更改爲: -

Ingredient item1 = new Ingredient ("Butter", 100); 
+1

謝謝你很多......我發誓我去年從compsci課程中學到的一切都在門外,如果你不使用它,你就會失去它!再次感謝:) –

+0

@MichelleFrazer ..沒問題,有時會發生。歡迎雖然:) –

0

只要你收集到關於一些符號時間誤差是找不到的,永遠記住,你已經在你的程序中使用了某些不存在存在或者它使用當前類路徑找到不能

1

你應該傳遞第二個參數爲int。您將字符串「100」更改爲數字100而不是「100」