我之前的问题:Java - 行不一致时将文本文件导入数组
每次我尝试使用 .split 或 .indexOf 时,都会收到一条错误消息,内容如下:“无法在数组类型 String[] 上调用 split(String, int)”。Eclipse 没有太大帮助,建议我将其更改为 .length
我的代码:
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++;
}
}
}
有人对如何解决这个问题有任何想法吗?