我正在尝试从文本块中的每一行中获取最长单词的列表。我无法从 processing.class 中的 main 获取要打印的信息。信息在 Tools.class 中处理。并且还有一个 Counters.class 涉及但它是不可变的。
但是,它给了我这个警告:The value of the local variable longestWords is not used
在我的计数器代码中,在我声明的行中longestWords =h;
。为什么会这样,我该如何解决?
这是我的代码:
(加工)
longestWords = stat.getLongestWords();
for(i = 0; i < longestWords.size(); i++){
System.out.print(longestWords.get(i));
}
抛出的错误:deol5210_a1.Processing.main(Processing.java:66) 处的线程“main”java.lang.NullPointerException 中的异常
指向 for(i = 0; i <longestWords.size(); i++){ (工具)
}else {
currentWord = lineScanner.next();
if(currentWord.contains("$")){
currentWord.replace("$", "");
moneySpent += Double.parseDouble(currentWord.replace("$", ""));
}else if (currentWord.length() > lineLongestWord.length()){
lineLongestWord = currentWord;
}
if(currentWord.length() > longestWord.length()){
longestWord = currentWord;
lineLongestWord = currentWord;
wordCount++;
} else {
wordCount++;
}
(计数器)
public ArrayList<String> getLongestWords(){
return longestWords;
}
public Counters(int a, double b, int c, int d, double e, double f, String g, ArrayList<String> h){
lineCount = a;
avgWordCount = b;
wordCount = c;
purchaseCount = d;
moneySpent = e;
avgSpent = f;
longestWord = g;
ArrayList<String> longestWords = h;
}