gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:34:59 27 4
gpt4 key购买 nike

我正在尝试编写一个程序来计算文件中每个字符的数量。它工作正常,直到到达撇号。然后我得到一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com