简体   繁体   English

正在扫描字符错误?

[英]Scanning for Character error?

I'm scanning a file and trying to track the amount of times a character comes up. 我正在扫描文件,并尝试跟踪字符出现的次数。

public static Map<Character, Integer> getCountMap(Scanner in){
    Map<Character, Integer> wordCountMap = new TreeMap<Character, Integer>();

    while (in.hasNext()){
        Character word = in.next().toLowerCase();
        //CHAR HAS BEEN SEEN, ADD
        if(wordCountMap.containsKey(word)){
            int count = wordCountMap.get(word);
            wordCountMap.put(word, count + 1);
        }
        //NEW CHAR, CREATE
        else    {
            wordCountMap.put(word, 1);
        }
    }
return wordCountMap;
}

I'm getting an error for Character word = in.next().toLowerCase(); 我遇到Character word = in.next().toLowerCase();的错误Character word = in.next().toLowerCase();

I checked java api, and characters definitely have access to toLowerCase() . 我检查了Java api,字符肯定可以访问toLowerCase() However the api for Scanners says 但是扫描仪的API说

hasNext() Returns true if this scanner has another token in its input. hasNext()如果此扫描程序的输入中包含另一个令牌,则返回true。

Does that mean a scanner can't scan through per character? 这是否意味着扫描仪无法按字符扫描? Shouldn't this just scan through the characters, adding them to the map and increasing the count each time it see's something? 这不应该只是扫描字符,将其添加到地图中,并在每次看到东西时增加计数吗?

Final note : This code runs just fine if every Character is replaced with String . 最后说明:如果每个Character都用String替换,则此代码可以正常运行。 I can get a word count no problem. 我可以数字没问题。 Char count, not so much. 字符数,不是很多。

Main Method (In case this is required) 主要方法 (如果需要)

public static void main(Character[] args) throws FileNotFoundException{

    //read the book into the map
    Scanner in = new Scanner(new File("moby.txt"));
    Map<Character, Integer> wordCountMap = getCountMap(in);


    for (Character word: wordCountMap.keySet()){
        int count = wordCountMap.get(word);
        if (count > OCCURRENCES){
            System.out.println(word + " occurs " + count + " times.");
        }
    }

}

According to the Javadocs for the next() method of java.util.Scanner : 根据Javadocsjava.util.Scannernext()方法

 public String next() 

Finds and returns the next complete token from this scanner. 查找并返回此扫描仪的下一个完整令牌。 A complete token is preceded and followed by input that matches the delimiter pattern. 完整的标记在其前面,然后是与定界符模式匹配的输入。

As can be seen, this method does not return Character ; 可以看出,此方法不返回Character it returns String , which is why you're getting that error. 它返回String ,这就是为什么您会收到该错误。

A token is basically a substring that falls between two instances of a delimiter. 标记基本上是一个位于分隔符的两个实例之间的子字符串。 The default delimiter of a Scanner is a whitespace object ( \\s , \\t , \\n , etc.). Scanner的默认定界符是空白对象( \\s\\t\\n等)。 So the Scanner goes through the file and each invocation of next() will return the next sequence of characters that is between what is seen as a delimiter. 因此,扫描程序会遍历文件,每次调用next()都会返回下一个字符序列,该序列介于被视为分隔符的字符之间。

So what you could do is change the delimiter so that the Scanner counts each character in the file as a token, though that's a bit complex. 因此,您可以做的是更改定界符,以使扫描程序将文件中的每个字符都计为令牌,尽管这有点复杂。 What you could do instead is make use of the fact that the String class has a method toCharArray() , which returns the sequence of characters in the string as an array. 相反,您可以利用String类具有toCharArray()方法的事实,该方法将字符串中的字符序列作为数组返回。 You can count the individual characters a lot easier that way: 您可以通过这种方式更轻松地计算单个字符:

String word = in.next().toLowerCase();
char[] charsInWord = word.toCharArray();
// ...

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

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