【问题标题】:Why does one println statement change the entire output of my code?为什么一个 println 语句会改变我的代码的整个输出?
【发布时间】:2019-04-01 17:21:58
【问题描述】:

问题

我目前正在创建一个程序来读取文件并查找几个变量。我遇到了这个问题,改变一个 println 会改变我的代码的整个输出。我以前从未遇到过这种情况,不确定这是 Eclipse 错误还是我的错误?

我的代码

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);


    }
}

但是

只需在第一个 Scanner 实例中更改System.out.println(word.next()) 即可更改整个输出。如果我把它留在里面,我会在底部得到三个打印语句以及我在寻找什么。如果我删除它,因为我不想在文件中打印每个单词,它在控制台中显示为空。不确定为什么 while 语句中的一个打印语句会更改整个输出。它首先存在的唯一原因是确保扫描仪以我想要的方式接收输入。

【问题讨论】:

    标签: java file file-io java.util.scanner


    【解决方案1】:

    不知道为什么while语句中的一个打印语句会改变整个输出

    因为当语句存在时,您正在使用扫描仪中的令牌。当它被注释掉时,你不是。消耗令牌的不是打印,而是对next() 的调用。

    注释掉后,你的循环是:

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

    hasNext() 不会修改扫描器的状态,因此如果进入循环体,永远循环。

    如果你想要一行你可以注释掉或者不注释掉,把代码改成:

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

    【讨论】:

    • @IanHank:是的,这将是最好的,尽管这确实与问题正交。
    【解决方案2】:

    通过使用System.out.println(word.next());,由于next() 方法,您正在循环浏览集合中的元素。因此,直接调用 next() 将允许您完成迭代。

    当注释掉//System.out.println(word.next()); 时,word.hasNext() 将导致您永远循环(只要有一个单词),因为您将无法移动到下一个令牌。

    下面的sn-p将帮助你达到你想要的结果

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

    【讨论】:

      【解决方案3】:

      不知道为什么要三遍阅读文本。但如果你真的需要,我会在打开下一个扫描仪之前关闭第一个扫描仪。

      【讨论】:

      • 虽然这通常是一个好主意,但我认为它不能回答为什么代码行为与注释掉的语句不同的问题。
      • 我通常不会,但是老师希望我们这样做,所以我想我们可以练习一下?实际上,我在发布后立即关闭了所有三个,并认为有人会对此发表评论。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 2011-09-04
      • 2022-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多