我正在尝试编写一个程序来计算文件中每个字符的数量。它工作正常,直到它到达撇号。然后我得到一个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);
}
}
这是正在阅读的样本(来自The Great 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。谢谢你的帮助!
答案 0 :(得分:1)
'’'
不在地图中。它与'\''
不同。