2013-04-04 100 views
2

我想創建一個小的庫存控制程序,其中每個StockItem有5個實例變量,一個itemID,一個itemDesc,一個價格,一個數量和一個reOrderLevel。我已經在StockItem文件中設置了我的構造函數,並且在嘗試創建測試程序文件以測試我的所有方法時遇到了問題。當我嘗試使用我的構造函數創建一個新的StockItem,我得到的錯誤:調用構造函數時數據類型不正確

要求:字符串,字符串,雙,INT,INT 發現:字符串,字符串,字符串,字符串,字符串

如何我可以修復這個錯誤嗎?我不確定它是否是我的構造函數中的錯誤,或者如果我的測試器文件中的代碼是錯誤的。

在此先感謝。

這裏是我的構造函數代碼:

public class StockItem { 

String itemID; 
String itemDesc; 
Double price; 
int quantity; 
int reOrderLevel; 
//declaring my instance variables  

public StockItem (String itemID, String itemDesc, Double price, int quantity, int reOrderLevel) {  
    this.itemID = itemID; 
    this.itemDesc = itemDesc; 
    this.price = price; 
    this.quantity = quantity; 
    this.reOrderLevel = reOrderLevel; 
} 

,這裏是我的測試代碼,到目前爲止,試圖創建一個項目:

public class StockItemTester { 

public static void main (String[] args) { 
    StockItem item1 = new StockItem ("ABC", "iPhone 5", " 500", "3", "10");  
}  

} 
+1

你知道一個方法/構造函數_signature_是什麼嗎? – Juvanis 2013-04-04 13:28:32

+0

在這種情況下,錯誤消息本身會爲您提供大量信息。 – midhunhk 2013-04-04 13:30:39

+1

在附註上,你真的想要一個'Double'對象而不是'double'原始類型嗎? – Quetzalcoatl 2013-04-04 13:39:53

回答

4

你必須根據構造函數簽名傳遞類型:

StockItem item1 = new StockItem ("ABC", "iPhone 5", 500.0, 3, 10); 
           ^^^^^ ^^^^^^^^^^ ^^^^^ ^^^ ^^^ 
           String String  Double int int 
           itemId itemDesc price qty reorderlevel 
+0

啊哈,我明白了,謝謝! – 2013-04-04 13:31:27

4

當您的構造函數需要不同的d時,您傳入String s ata類型,如int s和double s。

你需要讓你在正確的數據類型,通過改變你的構造:

new StockItem ("ABC", "iPhone 5", 500.0, 3, 10); // String, String, double, int, int 
2

你需要這樣做:

StockItem item1 = new StockItem ("ABC", "iPhone 5", 500, 3, 10);  

在函數調用中引號的任何值被視爲一個字符串。

+0

歡呼聲,那整理它! – 2013-04-04 13:32:01

2

最後兩個參數必須是類型int,以及之前的一個參數double根據您的類中的實例聲明。

StockItem item1 = new StockItem ("ABC", "iPhone 5", 500, 3, 10);

2

是的,你是路過5串到想要2串和3號的方法。

你的方法調用更改從:

StockItem item1 = new StockItem ("ABC", "iPhone 5", " 500", "3", "10"); 

到:

StockItem item1 = new StockItem ("ABC", "iPhone 5", 500, 3, 10); 
1

你不應該有3個左右的數值參數報價,即500和10的報價意味着他們是被認爲是String而不是double和兩個ints。

相關問題