简体   繁体   English

txt文件java中的字符出现

[英]Character occurrence in a txt file java

I'm writing a character occurrence counter in a txt file. 我正在txt文件中写一个字符出现计数器。 I keep getting a result of 0 for my count when I run this: 运行此命令时,我的计数始终为0:

  public double charPercent(String letter) {

        Scanner inputFile = new Scanner(theText);

        int charInText = 0;
        int count = 0;

        // counts all of the user identified character
        while(inputFile.hasNext()) {

            if (inputFile.next() == letter) {
                count += count;
            }

        }

        return count;
    }

Anyone see where I am going wrong? 有人看到我要去哪里了吗?

This is because Scanner.next() will be returning entire words rather than characters. 这是因为Scanner.next()将返回整个单词而不是字符。 This means that the string from will rarely be the same as the single letter parameter(except for cases where the word is a single letter such as 'I' or 'A'). 这意味着from的字符串很少与单个字母参数相同(除非单词是单个字母,例如“ I”或“ A”)。 I also don't see the need for this line: 我也看不到这一行的必要:

int charInText = 0;

as the variable is not being used. 因为没有使用该变量。

Instead you could try something like this: 相反,您可以尝试如下操作:

 public double charPercent(String letter) {

    Scanner inputFile = new Scanner(theText);

    int totalCount = 0;

    while(inputFile.hasNext()) {

        //Difference of the word with and without the given letter
        int occurencesInWord = inputFile.next().length() - inputFile.next().replace(letter, "").length();

        totalCount += occurencesInWord;

    }

    return totalCount;
}

By using the difference between the length of the word at inputFile.next() with and without the letter, you will know the number of times the letter occurs in that specific word. 通过使用inputFile.next()中带字母和不带字母的单词长度之间的差,您将知道字母在该特定单词中出现的次数。 This is added to the total count and repeated for all words in the txt. 这将添加到总数中,并对txt中的所有单词重复。

use inputFile.next().equals(letter) instead of inputFile.next() == letter1 . 使用inputFile.next().equals(letter)代替inputFile.next() == letter1

Because == checks for the references. 因为==检查引用。 You should check the contents of the String object. 您应该检查String对象的内容。 So use equals() of String 所以使用String equals()

And as said in comments change count += count to count +=1 or count++ . 如评论中所述,将count += count更改为count +=1count++

Read here for more explanation. 在这里阅读更多说明。

Do you mean to compare the entire next word to your desired letter? 您的意思是将整个下一个单词与所需字母进行比较吗?

inputFile.next() will return the next String, delimited by whitespace (tab, enter, spacebar). inputFile.next()将返回下一个由空格分隔的字符串(制表符,Enter和空格键)。 Unless your file only contains singular letters all separated by spaces, your code won't be able to find all the occurrences of letters in those words. 除非您的文件仅包含所有用空格分隔的单数字母,否则您的代码将无法找到这些单词中所有出现的字母。

You might want to try calling inputFile.next() to get the next String, and then breaking that String down into a charArray. 您可能想要尝试调用inputFile.next()以获取下一个String,然后将该String分解为charArray。 From there, you can iterate through the charArray (think for loops) to find the desired character. 从那里,您可以遍历charArray(考虑循环)以找到所需的字符。 As a commenter mentioned, you don't want to use == to compare two Strings, but you can use it to compare two characters. 正如评论员所提到的,您不想使用==比较两个字符串,但是可以使用它比较两个字符。 If the character from the charArray of your String matches your desired character, then try count++ to increment your counter by 1. 如果String的charArray中的字符与所需字符匹配,请尝试使用count++将计数器加1。

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

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