0

Exception in thread "main" java.util.NoSuchElementException
There has 500 lines in the document, but it only tokenize for 300++ lines. s3 = itr.nextToken(); // This is the line that got error. console print that the error at this line.

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);
            }
        }
    }
}
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
  • Could you please describe what do you want to achieve. Provide example of data that you have in the file `Airline.txt` and full stack trace. – SternK Dec 07 '19 at 07:59
  • Have you tried running it through the debugger to see what happens? – Marit Dec 07 '19 at 10:34
  • @SternK There have 500 tweets that already preprocess in the file. I want to do trigram tokenization. – 萌萌哒蚂蚁 Dec 07 '19 at 14:53
  • @Marit ya But i dont have idea what to do – 萌萌哒蚂蚁 Dec 07 '19 at 14:54
  • @萌萌哒蚂蚁 I tried to run your code and It works fine when each line in the input file has three chunks, but when less I get the java.`util.NoSuchElementException`. So, please check your input file. – SternK Dec 07 '19 at 18:51
  • @萌萌哒蚂蚁 For making some suggestion related to possible correction of your solution I should clear understand the format of input file. – SternK Dec 07 '19 at 19:03

0 Answers0