简体   繁体   中英

Why does one println statement change the entire output of my code?

Problem

I am currently creating a program to read a file and find a couple of variables. I am running into this problem where changing one println changes the entire output of my code. I have never run into this before and am not sure if this is an eclipse error or my error?

My Code

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FileAnalyzer {
    public static void main(String args[]) throws IOException {
        Scanner input = new Scanner(System.in);
        String fileName;
        int words = 0, letters = 0, blanks = 0, digits = 0, miscChars = 0, lines = 0;

        System.out.print("Please enter the file path of a .txt file: ");
        fileName = input.nextLine();

        File text = new File(fileName);
        //System.out.println(text.exists());

        Scanner word = new Scanner(text);
        while(word.hasNext()) {
            //System.out.println(word.next());
            words++;
        }
        word.close();

        Scanner letter = new Scanner(text);
        while(letter.hasNext()) {
            String currentWord = letter.next().toLowerCase();
            for(int i = 0; i < currentWord.length(); i++) {
                if(Character.isLetter(currentWord.charAt(i))) {
                    letters++;
                }
            }
        }
        letter.close();

        Scanner blank = new Scanner(text);
        while(blank.hasNextLine()) {
            String currentWord = blank.nextLine();
            for(int j = 0; j < currentWord.length(); j++) {
                if (currentWord.charAt(j) == ' ') {
                    blanks++;
                }
            }
        }
        blank.close();

        System.out.println("Words: " + words);
        System.out.println("Letters: " + letters);
        System.out.println("Blanks: " + blanks);


    }
}

However

Simply changing System.out.println(word.next()) in the first Scanner instance changes the entire output. If i leave it in I get the three print statements at the bottom and what I am looking for. If I remove it since I do not want each word printed in the file it shows as nothing in the console. Not Sure why one print statement within a while statement changes the entire output.The only reason it was there in the first place was to make sure the scanner was taking input the way I had wanted.

Not Sure why one print statement within a while statement changes the entire output

Because when the statement is present, you're consuming a token from the scanner. When it's commented out, you're not. It's not the printing that consumes the token, it's the call to next() .

With it commented out, your loop is:

while (word.hasNext()) {
    words++;
}

hasNext() doesn't modify the state of the scanner, so that will just loop forever if it goes into the loop body at all.

If you want to have a line you can comment out or not, change the code to:

while (word.hasNext()) {
    String next = word.next(); // Consume the word
    System.out.println(next); // Comment this out if you want to
    words++;
}

By using System.out.println(word.next()); you are cycling through the elements in a collection due to the next() method. So invoking next() directly will allow you to move through the iteration.

When commenting out //System.out.println(word.next()); , then word.hasNext() will cause you to loop forever(provided there is a word) as you will not be able to move to the next token.

The below snippet will help you achieve your desired result

while(word.hasNext()){
   word.next();
   words++;
}

Not sure why you would want to go thru the text three times. But if you really have to, I would close the first scanner before opening the next.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM