只是想知道我做了什麼錯在這裏,我的方法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());
}
}
'state = line.split(「=」)'你得到的錯誤是因爲'state'的類型爲'State []',而'line.split(「=」)'返回一個String []'。您不能在任意類型之間自由分配。你必須遍歷結果的String數組,並使用這些元素來自己分析'State'對象。 – QBrute
你能否提供類State的代碼? – Karan
@Karan爲什麼?你認爲'國家'會繼承'字符串'? '字符串'是'最後',所以不太可能。 – AxelH