Jess
Jess

Reputation: 1545

java.util.Scanner issue: stops reading after certain number of characters?

I'm using Scanner to read in approx 700 lines of text (one word per line), and storing each line/word as an element of String ArrayList constructed with a predetermined 800-element size. However, while debugging in Eclipse's variable mode I noticed that the later elements of the ArrayList weren't initialized at all - it seems Scanner stopped in the middle of a word, on a random line.

I'm using try with resources:

try (Scanner stopwordImport = new Scanner(new File(stopwordFile))) {
        while (stopwordImport.hasNext()) {
            stopWords.add(stopwordImport.nextLine());
        }

...so Scanner should be closing.

I'm at a loss...what's going wrong? Thanks in advance.

Upvotes: 0

Views: 580

Answers (1)

Phoebe
Phoebe

Reputation: 11

try FileReader instead of File, so you don't have buffer problems. I had the same problem and this worked for me.

Upvotes: 1

Related Questions