2013-11-28 41 views
0

在我的程序中,用戶聲明一串數字,我試圖找出數組變成數組。
實施例:將聲明的字符串轉換爲數組

WeeklyFiber week2 =新WeeklyFiber( 「CS4567」, 「13年11月24日」,32 「27,26,28 」);

我試圖找出如何將該字符串添加到我的類實例變量。
這是我有:

private String sampleID; 
    private String weekOfTest; 
    private int engineerID; 
    private String[] strengths = new String[20]; 
    private static int count; 

    public WeeklyFiber(String sampleID, String weekOfTest, int engineerID, String strengths) 
    { 
     this.sampleID = sampleID; 
     this.weekOfTest = weekOfTest; 
     this.engineerID = engineerID; 
     this.strengths = strengths; 
     count++; 
    } 

我的編譯錯誤消息指出不兼容的類型,需要:的String [],發現:字符串

+0

您正試圖在構造函數中將字符串賦值給String [],這就是爲什麼編譯器會給這個 – Deepak

回答

0

這是因爲你已經宣佈的String []的優勢這是一個陣列。

聲明你的構造是這樣的:

public WeeklyFiber(String sampleID, String weekOfTest, int engineerID, String[] strengths) 
    { 
     this.sampleID = sampleID; 
     this.weekOfTest = weekOfTest; 
     this.engineerID = engineerID; 
     this.strengths = strengths; 
     count++; 
    } 

撥打電話,如:

WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, new String[] {"27","26", "28"}); 
+0

你是對的,我需要改變我的構造函數。謝謝! – JessNicole27

+0

如果您認爲答案是正確的,請註冊並將其標記爲正確。 – anon

0

傳遞這樣的:

WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, 
          new String[] { "27", "26", "28" }); 
0

你需要解析數字String到多個String s。例如,

this.strengths = strengths.split(","); 
0

你不能說this.strengths = strengths因爲strengths說法是String型的,而不是String[]。那是你的錯誤來自何處。