0

我有一个任务让我很早就很难过,其余的任务相当容易(一旦导入数据就对其进行排序,然后以不同的名称再次保存)。

我们需要将 .txt 文件中的数据导入 3 个单独的数组(名称、吉祥物、别名),但行不一致。一致我的意思是一行可能有:

    Glebe,G Shield,Glebe District

而另一行可能有:

    St George,Knight & Dragon,Saints,Dragons,St George Illawarra

first 之前的所有内容都属于 name 数组。

第一个之后,第二个之前的所有东西都属于吉祥物数组。

second 之后的所有内容,直到行尾都属于别名数组。

我已经能够弄清楚如何导入包含整行的 .txt 文件,然后我能够将其转换为导入“,”和新行之前的所有内容(使用分隔符)。然而,包含超过 3 组数据的行会破坏导入,因为别名数组最终只包含 1 而不是其他所有内容。

因此,有谁知道并可以向我展示一个几乎可以做到的代码:

name = 第一个之前的所有内容,

吉祥物 = 第一个之后,第二个之前的所有东西,

Alias = 第二个之后的所有内容,直到行尾

我可以用它作为我的基础吗?

经过一天的研究,我不断地想出死胡同。它们通常都涉及在每个逗号处拆分,但这会破坏导入(具有超过 1 个别名的行,第二个别名被放入名称数组等)

这是我提出的将整行导入数组的代码:

    public static void LoadData() throws IOException
{
    String clubtxt = ("NRLclubs.txt");
    String datatxt = ("NRLdata.txt");
    int i, count;

    File clubfile = new File(clubtxt);
    File datafile = new File(datatxt);

    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();
    }
    else
    {
        System.out.println("\n" + "The file " + clubfile + " does not exist." + "\n");
    }

    if (datafile.exists())
    {
        count = 0;
        Scanner inputFile = new Scanner(datafile);
        i = 0;
        while(inputFile.hasNextLine())
        {
            count++;
            inputFile.nextLine();
        }
        String [] teamdata = new String[count];
        inputFile.close();
        inputFile = new Scanner(datafile);
        while(inputFile.hasNext())
        {
            teamdata[i] = inputFile.nextLine();
            System.out.println(teamdata[i]);
            i++;
        }
        inputFile.close();
    }
    else
    {
        System.out.println("\n" + "The file " + datafile + " does not exist." + "\n");
    }
}
4

5 回答 5

1

由于您只想解析前 2 个逗号,因此可以使用split带限制的 String。

如果您愿意,可以使用 StringindexOf方法查找前 2 个逗号,然后使用 Stringsubstring方法获取逗号之间的字符。

您希望能够处理带有一个逗号或根本没有逗号的行。

这是解析字符串行的一种方法

public List<String> splitLine(String line) {
    List<String> list = new ArrayList<String>();
    int firstPos = line.indexOf(",");
    int secondPos = line.indexOf(",", firstPos + 1);
    if (firstPos >= 0) {
        if (secondPos >= 0) {
            list.add(line.substring(0, firstPos));
            list.add(line.substring(firstPos + 1, secondPos));
            list.add(line.substring(secondPos + 1));
        } else {
            list.add(line.substring(0, firstPos));
            list.add(line.substring(firstPos + 1));
            list.add("");
        }
    } else {
        list.add(line);
        list.add("");
        list.add("");
    }

    return list;
}
于 2013-05-29T13:31:58.797 回答
1

查看String.split带有参数的方法limit

当您将输入行放在名为 的变量中line时,您可以调用

String[] tokens = line.split(',', 3);

这将在逗号上分割行,同时确保它不会返回超过 3 个标记。它返回一个字符串数组,其中第一个元素是第一个逗号之前的内容,第二个是第一个和第二个逗号之间的内容,第三个元素是第二个逗号之后的内容。

于 2013-05-29T13:22:53.673 回答
0

基本上你想要做的是在读入时将每一行拆分为一个数组,然后逐行解析数据。像这样的东西(伪代码):

Scanner inputFile = new Scanner(datafile);  
while(inputFile.hasNextLine()) {  
  String line = inputFile.nextLine();  
  String[] lineSplit = line.split(",");  
  //TODO: make sure lineSplit is at least 3 long.  
  String name = lineSplit[0];  
  String mascot = lineSplit[1];

  //EDIT: Don't just get the last element, get everything after the first two.
  // You can do this buy just getting the substring of the length of those two strings
  // + 2 to account for commas. 
  //String alias = lineSplit[lineSplit.length() - 1]; 
  String alias = line.substring(name.length() + mascot.length() + 2);

  //If you need to do trimming on the strings to remove extra whitespace, do that here:
  name = name.trim();
  mascot = mascot.trim();
  alias = alias.trim();

  //TODO: add these into the arrays you need.  
}

希望这可以帮助。

于 2013-05-29T13:28:07.200 回答
0

尝试查看 Pattern/Matcher 的东西——你需要想出一个合适的正则表达式。

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

这样的事情可能会做到这一点:

static final Pattern pattern = Pattern.compile("([^,]*),([^,]*),(*$)");
MatchResult result = pattern.matcher(line).toMatchResult();
if (result.groupCount() == 3) {
    // Found the groups
    name = result.group(0);
    // etc..
} else {
    // failed to match line
}
于 2013-05-29T13:28:48.760 回答
0

您可以使用String.split方法。

String line = // the line you read here

// Split on commas but only make three elements
String[] elements = line.split(',', 3);

// The first belongs to names
names[linecount] = elements[0];
// The second belongs to mascot
mascot[linecount] = elements[1];
// And the last belongs to aliases
aliases[linecount] = elements[2];
于 2013-05-29T13:27:06.403 回答