2017-10-19 21 views
0

只是想知道我做了什麼錯在這裏,我的方法setLine()這是得到一個錯誤:的String []不能轉化爲國家[]

error: incompatible types: String[] cannot be converted to State[]

我不是什麼做的不太清楚修復它,因爲我需要將行分割並存儲在該狀態數組中,以便從csv文件讀取時可以確定它是否是狀態或位置。

public static void readFile(String inFilename) 
{ 
    FileInputStream fileStrm = null; 
    InputStreamReader rdr; 
    BufferedReader bufRdr; 
    int stateCount = 0, locationCount = 0; 
    String line; 

    try 
    { 
     fileStrm = new FileInputStream(inFilename); 
     rdr = new InputStreamReader(fileStrm); 
     bufRdr = new BufferedReader(rdr); 
     line = bufRdr.readLine(); 
     while (line != null) 
     { 
      if (line.startsWith("STATE")) 
      { 
       stateCount++; 
      } 
      else if (line.startsWith("LOCATION")) 
      { 
       locationCount++; 
      } 
      line = bufRdr.readLine(); 
     } 
     fileStrm.close(); 

     State[] state = new State[stateCount]; 
     Location[] location = new Location[locationCount]; 
    } 
    catch (IOException e) 
    { 
     if (fileStrm != null) 
     { 
      try { fileStrm.close(); } catch (IOException ex2) { } 
     } 
     System.out.println("Error in file processing: " + e.getMessage()); 
    } 
} 

public static void processLine(String csvRow) 
    { 
     String thisToken = null; 
     StringTokenizer strTok; 
     strTok = new StringTokenizer(csvRow, ":"); 
     while (strTok.hasMoreTokens()) 
     { 
      thisToken = strTok.nextToken(); 
      System.out.print(thisToken + " "); 
     } 
     System.out.println(""); 
    } 

public static void setLine(State[] state, Location[] location, int stateCount, int locationCount, String line) 
{ 
    int i; 
    state = new State[stateCount]; 
    state = line.split("="); <--- ERROR 
    for(i = 0; i < stateCount; i++) 
    { 
    } 
} 

public static void writeOneRow(String inFilename) 
{ 
    FileOutputStream fileStrm = null; 
    PrintWriter pw; 
    try 
    { 
     fileStrm = new FileOutputStream(inFilename); 
     pw = new PrintWriter(fileStrm); 
     pw.println(); 
     pw.close(); 
    } 
    catch (IOException e) 
    { 
     if (fileStrm != null) 
     { 
      try 
      { 
       fileStrm.close(); 
      } 
      catch (IOException ex2) 
      {} 
     } 
     System.out.println("Error in writing to file: " + e.getMessage()); 
    } 

} 
+0

'state = line.split(「=」)'你得到的錯誤是因爲'state'的類型爲'State []',而'line.split(「=」)'返回一個String []'。您不能在任意類型之間自由分配。你必須遍歷結果的String數組,並使用這些元素來自己分析'State'對象。 – QBrute

+1

你能否提供類State的代碼? – Karan

+0

@Karan爲什麼?你認爲'國家'會繼承'字符串'? '字符串'是'最後',所以不太可能。 – AxelH

回答

3

出現此錯誤,因爲它只是說:'String[]不能轉換到State[]'。這就像你想將Integer存儲到String中一樣,因爲這些類型彼此之間沒有關係(父 - >子)。

所以,如果你想解決你的問題,你需要一種方法,將String[]轉換爲State[]。類似這樣的:

private State[] toStateArray(String[] strings){ 
    final State[] states = new State[strings.length]; 
    for(int i = strings.length-1; i >= 0; i--){ 
     states[i] = new State(strings[i]); // here you have to decide how to convert String to State 
    } 
    return states; 
} 
+0

感謝您的回覆和幫助。我試圖將其添加到我的代碼中,現在我得到for循環中的錯誤行:**構造函數State類中的狀態不能應用於給定的類型; ** 代碼行:'states [i] =新狀態(字符串[i]);' – John

+0

是的,那是因爲這個構造函數('State(String)')不存在。現在你已經決定了需要使用哪個構造函數,但我的例子應該給你這個想法;) – Lino