我仔细阅读了许多其他 NPE 解决方案,并且我尝试实施其他建议,但没有一个完全符合我正在尝试做的事情,它只会导致更多的 eclipse 错误。我已经编译并尝试从命令行运行,在命令行运行时为我正在运行的应用程序提供了几个字符串。下面是主类,以及包含主使用的方法的类。
使用 main 方法的类:
package my.package.ext;
public class WordCounterApp {
/**
* @param args
* Two command line arguments: the first one needs to be in quotes, the string that will be used, the second optional argument
* is the unique word to be counted (countWord method).
* @param source
* @param word
*/
public static void main(String[] args) {
String source = null;
String uniqueword = null;
StringBuilder word = null;
WordCounter counter = new WordCounter(source, word);
WordCounter uniqueCounter = new WordCounter(source, uniqueword);
counter.countWords(source);
counter.countUniqueWords(source);
uniqueCounter.countWord(source, uniqueword);
}
}
使用其他方法进行类:
package my.package.ext;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Character;
import java.lang.StringBuilder;
public class WordCounter {
public Integer counter = 0;
public String source;
public HashSet<String> hashset;
public StringBuilder word;
public String uniqueword;
public WordCounter(String source) {
counter = new Integer(counter);
}
public WordCounter(String source, StringBuilder word) {
counter = new Integer(counter);
}
public WordCounter(String source, String uniqueword) {
counter = new Integer(counter);
}
/**
*
* @param line - the string parameter to get a total word count from.
*/
public int countWords(String source) {
boolean word = false;
int endOfLine = source.length() - 1;
Integer counter = 0;
for (int i = 0; i < source.length(); i++) {
if (Character.isLetter(source.charAt(i)) == true && i != endOfLine) {
word = true;
//} else if (Character.charValue(line.charAt(i)) == "-" && i != endOfLine) {
// word = true;
} else if (Character.isLetter(source.charAt(i)) == false && word == true) {
counter++;
word = false;
} else if (Character.isLetter(source.charAt(i)) && i == endOfLine) {
counter++;
}
}
System.out.println(counter);
return counter;
}
/**
*
* @param line - the string parameter that we will return the unique word count from. Randy recommends a HashSet.
* Put it into a hashset. Hashsets don't allow duplicate elements. Then do a count.
*/
public int countUniqueWords(String line) {
hashset = new HashSet<String>();
word = new StringBuilder();
int endOfLine = line.length() - 1;
boolean isWord = false;
String stringWord = null;
Integer counter = 0;
for (int i = 0; i < line.length(); i++) {
if (Character.isLetter(line.charAt(i)) == true && i != endOfLine) {
//System.out.println(i);
word.append(line.charAt(i));
isWord = true;
} else if (Character.isLetter(line.charAt(i)) == false && isWord == true) {
counter++;
//System.out.println("Counter is: " + counter);
stringWord = word.toString();
//System.out.println("stringWord is now: " + stringWord);
hashset.add(stringWord);
//System.out.println(hashset);
word = new StringBuilder();
isWord = false;
} else if (Character.isLetter(line.charAt(i)) && i == endOfLine) {
counter++;
stringWord = word.toString();
hashset.add(stringWord);
}
}
//System.out.println(counter);
System.out.println("There are " + hashset.size() + " unique words in this string");
System.out.println("These are the unique words in the string: " + hashset);
return counter;
}
/**
*
* @param source - the string the word is to be counted from
* @param word - the word to be counted
*
*/
public void countWord(String source, String word) {
String str = source;
Pattern p = Pattern.compile("\\s"+word+"\\s");
Matcher m = p.matcher(str);
int count = 0;
while (m.find()) {
count++;
}
System.out.println("The word: " + "\"" + word + "\"" + " appears " + count + " times.");
}
}
我在这里确定了 NPE 的来源:
public int countWords(String source) {
boolean word = false;
int endOfLine = source.length() - 1; //the source of the NPE is this line
Integer counter = 0;
所以看着那个,我想我没有正确初始化源。我尝试过类似 WordCounter source = new WordCounter()
但是我尝试的每一个变体,引入正确的构造函数,都会给我其他 eclipse 错误。我似乎无法到达那里,我担心我走错了路。我可能在这里也有其他问题。我也不确定从命令行运行的正确方法,同时将一些字符串作为参数传递给方法。提前致谢