java - 撇号未被识别为 HashMap 中的现有键

标签 java string

我正在尝试编写一个程序来计算文件中每个字符的数量。它工作正常,直到到达撇号。然后我得到一个 nullPointerException ,我认为它发生是因为 HashMap 中的撇号键不存在,尽管我可能是错的,因为 new CollectionOfLetters().getAlphabet().containsKey('\'') 返回 true。

public class Letter {
    private int total; //number of occurrences of the letter
    private char letter;

    public Letter(char letter) {
        this.letter = letter; //instantiate the letter
    }
    public void incrementTotal() {
        this.total++;
    }
}

包含所有可能的字符的类

public class CollectionOfLetters {
    HashMap<Character, Letter> alphabet;
    int totalLetterCount;

    public CollectionOfLetters() {
        this.alphabet = new HashMap<>(32);
        for (char i = 'a'; i <= 'z'; i++) { //instantiate the collection of letters with the characters a through z
            alphabet.put(i, new Letter(i));
        }
        alphabet.put('.', new Letter('.')); //Add in a few more possible characters and symbols can exist in the text sample
        alphabet.put(' ', new Letter(' '));
        alphabet.put('?', new Letter('?'));
        alphabet.put('!', new Letter('!'));
        alphabet.put(',', new Letter(','));
        alphabet.put('\'', new Letter('\''));
    }

    public HashMap<Character, Letter> getAlphabet() {
        return alphabet;
    }

    public void incrementTotalLetterCount(){
        this.totalLetterCount++;
    }

    public void printClass() { //basically println(toString()) for this class 
        for (Map.Entry<Character, Letter> entry : alphabet.entrySet()) {
            System.out.println(entry.getKey() + "/" + entry.getValue().getLetter() + "/" + entry.getValue().getFrequency());
        }
    }
}

我编写的用于从txt文件读取到collectionOfLetters类中的类

public class ReaderOfFiles {
    FileReader reader;
    BufferedReader in;

    public void buildCollectionOfLetters(String fileName, CollectionOfLetters letters) {
        String line;
        try {
            reader = new FileReader(fileName);
            in = new BufferedReader(reader);
            line = in.readLine();

            do {
                for (int i = 0; i < line.length(); i++) {
                    System.out.println(Character.toLowerCase(line.charAt(i))); //debugging line to print the character that's being read right now
                    letters.getAlphabet().get(Character.toLowerCase(line.charAt(i))).incrementTotal(); //this is the problematic line according to eclipse.
                    //nullPointerException when line.charAt(i) == '\''
                    letters.incrementTotalLetterCount();
                }
            } while ((line = in.readLine()) != null); //as long as the last line hasn't been reached

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }
}

主类

public class Driver {

    public static void main(String[] args) {
        CollectionOfLetters COL = new CollectionOfLetters();
        ReaderOfFiles ROF = new ReaderOfFiles();
        COL.printClass(); //see output results below
        ROF.buildCollectionOfLetters("/Users/fnord/Documents/workspace/Cryptography/src/GGTest.txt", COL);
    }
}

这是正在读取的示例(来自《了不起的 Gatsby 》):

! .?we’re descended from the Dukes of Buccleuch, but the actual founder of my line was my grandfather’s brother who came here in fifty-one, sent a substitute to the Civil War and started the wholesale hardware business that my father carries on today.

这是从上面收到的输出。

 / /0.0
a/a/0.0
!/!/0.0
b/b/0.0
c/c/0.0
d/d/0.0
e/e/0.0
f/f/0.0
g/g/0.0
'/'/0.0
h/h/0.0
i/i/0.0
j/j/0.0
k/k/0.0
l/l/0.0
,/,/0.0
m/m/0.0
n/n/0.0
././0.0
o/o/0.0
p/p/0.0
q/q/0.0
r/r/0.0
s/s/0.0
t/t/0.0
u/u/0.0
v/v/0.0
w/w/0.0
x/x/0.0
y/y/0.0
z/z/0.0
?/?/0.0
true
!

.
?
w
e
’
Exception in thread "main" java.lang.NullPointerException
    at utils.ReaderOfFiles.buildCollectionOfLetters(ReaderOfFiles.java:24)
    at driver.Driver.main(Driver.java:13)

'\'' 在 'g' 和 'h' 之间打印出来,我认为这意味着它应该存在,所以我不确定为什么会收到 nullPointerException。感谢您的帮助!

最佳答案

''' 不在 map 中。它与 '\'' 是不同的字符。

关于java - 撇号未被识别为 HashMap 中的现有键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30091002/

相关文章:

java - 使用 IKVMC 将 Java 转换为 .NET 库 - 警告 IKVMC0108 : not a class file

java - 了解队列插入

r - 在 R 中替换单个反斜杠

php - 查找数组中重复次数最多的子字符串

C# 字符串操作 : From "TABLE_NAME" to "TableName"

java - 使用java注销计算机

java - 线程(textView 和进度条)

Java servlet Filter 在 FilterConfig 中具有多个值?是否可以?

c++ - 清理字符串以打印到终端的惯用方法?

python - 如何在尚未被空格包围的破折号前后插入空格?