简体   繁体   English

不使用局部变量字的值

[英]The value of the local variable word is not used

I have a method that has a Scanner as a parameter. 我有一个方法以Scanner作为参数。 The purpose of the method is to count the number of words in a file and return that number. 该方法的目的是计算文件中的单词数并返回该数字。 Here is the method: 方法如下:

public static int getWordCount(Scanner inputFile) 
        throws FileNotFoundException  {
    int wordCount = 0;      
    while(inputFile.hasNext()) {
        String word = inputFile.next();         
        wordCount++;                
    }
    return wordCount;       
}

The method works fine, however I get a "The value of the local variable word is not used" warning which I would like to get rid of. 该方法可以正常工作,但是我想摆脱此警告:“未使用局部变量字的值”警告。 I understand why I am getting the warning, however, as far as I know, I need the 我了解为什么收到警告,但据我所知,我需要

String word = inputFile.next(); 

to move the pointer through the file. 在文件中移动指针。 I can suppress the warning, however, I figure that there is probably a better way of doing what I am trying to do. 我可以取消警告,但是,我认为可能有更好的方法来完成我要尝试的操作。 I can only use Scanner class to read the file. 我只能使用Scanner类读取文件。

What you are doing is fine, but the warning is given because you only assign to word and never read from it. 您正在执行的操作很好,但是会发出警告,因为您仅分配给word而从未从word读取内容。 Try just leaving out the assignment and using just inputFile.next() instead with simply not saving the result. 尝试inputFile.next()分配,只使用inputFile.next()而不保存结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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