我剛纔的問題:Java - Importing text file into array when lines are not consistent.split和.indexOf在String []數組
每次我試圖努力.split或.indexOf我相處的線一條錯誤消息:「無法調用分裂(字符串, int)在數組類型String []'上。 Eclipse是不是太大的幫助,建議我改變它。長度
我的代碼:
import java.util.*;
import java.io.*;
public class Club
{
Scanner ConsoleInput;
public int count;
public Club() throws IOException
{
String clubtxt = ("NRLclubs.txt");
int i;
File clubfile = new File(clubtxt);
if (clubfile.exists())
{
count = 0;
Scanner inputFile = new Scanner(clubfile);
i = 0;
while(inputFile.hasNextLine())
{
count++;
inputFile.nextLine();
}
String[] teamclub = new String[count];
inputFile.close();
inputFile = new Scanner(clubfile);
while(inputFile.hasNext())
{
teamclub[i] = inputFile.nextLine();
System.out.println(teamclub[i]);
i++;
}
inputFile.close();
SplitClubdata(teamclub, count);
}
else
{
System.out.println("\n" + "The file " + clubfile + " does not exist." + "\n");
}
}
public void SplitClubdata(String[] teamclub, int count)
{
String[] line = teamclub;
int maxlines = count;
count = 0;
while(count <= maxlines)
{
// Split on commas but only make three elements
String elements[] = line.split(",", 3);
String names[] = new String[maxlines];
String mascot[] = new String[maxlines];
String aliases[] = new String[maxlines];
// The first belongs to names
names[count] = elements[0];
// The second belongs to mascot
mascot[count] = elements[1];
// And the last belongs to aliases
aliases[count] = elements[2];
count++;
}
}
}
任何人有關於如何解決這一問題的任何想法?
你有沒有考慮使用'List'([ArrayList的(http://docs.oracle.com/javase/6/docs/api/java /util/ArrayList.html)總是一個很好的選擇)讀取行?這將阻止你不得不讀取文件兩次(一次數,再次存儲)。 – Supericy
Eclipse很明顯「不能在數組類型String []上調用split(String,int)」,這意味着你的'line'是一個字符串數組,並且他只期望一個字符串。 – TecHunter