0

线程“main”中的异常 java.util.NoSuchElementException
文档中有 500 行,但它只标记了 300++ 行。s3 = itr.nextToken(); // 这是出错的行。控制台打印此行的错误。

public class bigram {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("Airline.txt"));
        FileOutputStream out = new FileOutputStream("Airline2.txt");

        ArrayList < String > bigrams = new ArrayList < String > ();

        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }

            StringTokenizer itr = new StringTokenizer(line);
            if (itr.countTokens() > 1) {
                System.out.println("String array size : " + itr.countTokens());
                String s1 = "";
                String s2 = "";
                String s3 = "";
                String s4 = "";
                while (itr.hasMoreTokens()) {
                    if (s1.isEmpty())
                        s1 = itr.nextToken();
                    if (s2.isEmpty()) {
                        s2 = itr.nextToken();
                    }
                    s3 = itr.nextToken(); // This is the line that got error.
                    s4 = "'" + s1 + "_" + s2 + "_" + s3 + "'";
                    bigrams.add(s4);
                    s1 = s2;
                    s2 = s3;
                    s3 = "";
                }

            } else
                System.out.println("Tokens is 1 or 0");
            int i = 0;
            while (i < bigrams.size()) {
                System.out.println(bigrams.get(i));
                i++;
            }
            //bigrams = Arrays.asList(i);
            for (String s: bigrams) {
                PrintStream p = new PrintStream(out);
                p.println(s);
                System.out.println(s);
            }
        }
    }
}
4

0 回答 0