0

我已经编写了代码来读取文件的第三列(treeout1.txt)并将这些内容写入另一个文件(tree.txt)。现在我想打开tree.txt并将内容写入stem.txt,其中tree.txt 在每一行中包含一个单词,并且在每一行的末尾找到一个分隔符。我已在下面附加了该 txt 文件。您可以查看它以更好地理解。现在我想将这些单词写入一行直到找到分隔符“###”...例如“女孩自己的论文”和下一行 vol 等等....我已经尝试过了,但是 ArrayIndexOutOfBoundsException 出现在 []...为什么?以及如何解决?

文本文件tree.txt如下

the
girl
own
paper
###
vol
###
viii
###
no
###
@card@
###
October
@card@
@card@
###
price
one
penny
###
as
the
baron
have
conjecture
the
housemaid
whom
he
have
call
out
of
the
nursery
to
look
for
###
lons
cane
on
find
her
master
have
go
without
it
do
not
hurry
back
but
stop
talk
###

代码:

 package simple;

 import java.io.*;
 import java.util.Scanner;
 import java.util.StringTokenizer;

 public class Simple {

   public static void main(String[] args) throws IOException {
     String line;
     String line2;
     String[] a = new String[100];
     int i = 0;
     try {
       BufferedReader br = new BufferedReader(new FileReader("C:/TreeTagger/treeout1.txt"));
       BufferedWriter output = new BufferedWriter(new FileWriter("D:/tree.txt"));
       String separator = System.getProperty("line.separator");
       while ((line = br.readLine()) != null) {
         StringTokenizer st2 = new StringTokenizer(line, "\n");
         while (st2.hasMoreElements()) {
           String line1 = (String) st2.nextElement();
           String[] array = line1.split("\\s+", 3);
           //System.out.println(array[2]);
           output.append(array[2]);
           output.newLine();
         }
       }
       output.close();
       br.close();
       BufferedReader br1 = new BufferedReader(new FileReader("D:/tree.txt"));
       BufferedWriter out = new BufferedWriter(new FileWriter("D:/stem.txt"));
       while ((line2 = br1.readLine()) != null) {
         StringTokenizer st = new StringTokenizer(line2, " ");
         while (st.hasMoreTokens()) {
           String element = st.nextToken();
           System.out.println(element);
           while (element != "###") {

             a[i] = element;
             i++;
           }
           out.append(a[i]);
           element = element.replace(element, "");

         }

       }
     } catch (IOException e) {

     }
   }
 }
4

1 回答 1

0

i to 0找到###分隔符后需要重新设置。否则,您将继续增加i,直到它大于 100(a的最大值)。

您也不能!=在字符串上使用运算符(来自您的代码:)element != "###"。您需要使用以下内容:

!"###".equals(element);
于 2013-11-04T04:12:57.490 回答