StringTokenizer doesn't read the firs line of the file.txt

60 Views Asked by At

I'm trying to take every single words from a text file and put them into a ArrayList but the StringTokenizer doesn't read the first line of the text file... What's wrong?

public class BufferReader {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        BufferedReader reader = new BufferedReader(new FileReader("C://Java-projects//EsameJava//prova.txt"));
                

        String line = reader.readLine();

        List<String> str = new ArrayList<>();

        while ((line = reader.readLine()) != null) {
            StringTokenizer token = new StringTokenizer(line); 

            while (token.hasMoreTokens()) {
                str.add(token.nextToken());
            }
        }

        System.out.println(str);

The only solution I found is to start the text file from the second line but it's not what I want...

1

There are 1 best solutions below

0
g00se On

This is how you could marry the (very) old and the new(er) to provide a collection of words:

import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class WordCollector {

    public static void main(String[] args) {
        try {
            List<String> words = WordCollector.getWords(Files.lines(Paths.get(args[0])));
            System.out.println(words);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static List<String> getWords(Stream<String> lines) {
        List<String> result = new ArrayList<>();
        BreakIterator boundary = BreakIterator.getWordInstance();
        lines.forEach(line -> {
            boundary.setText(line);

            int start = boundary.first();
            for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
                String candidate = line.substring(start, end).replaceAll("\\p{Punct}", "").trim();
                if (candidate.length() > 0) {
                    result.add(candidate);
                }
            }
        });
        return result;
    }
}