我确实编写了一个将文本文件作为输入的代码,然后程序使字符串数组与文本相等,以便可以比较单词并找到+计数唯一和重复的单词。该程序编译成功,但如果我尝试执行它,我会遇到问题:线程“main”中的异常java.lang.NullPointerException ... java:52。问题一定出在我声明字符串的方式上。我应该怎么写呢?谢谢!
import java.util.*;
class TextAnalyze{
public static void main(String[] args){
In read = new In ("text1.txt"); //input *.txt file
int tWords = 0; // counter, total words in the text file
int unW = 0; //counter UNIQUE words in the text file
String[] word = new String[31000]; //array with all the words
String[] word2 = new String[31000]; // array with all the words, used to compare
//String uniqueWords[] = new String[31000]; //array with the unique words
//int numberuniqueWords[] = new int [31000];
while(read.endOfFile() == false) {
word[tWords] = read.inWord();
word2[tWords] = word[tWords];
tWords++;
}
int totalWords = word.length;
int totalWords2 = word2.length;
List<String> uniqueWords = new ArrayList<>();
for (int i = 0; i < totalWords; i++) { // loop of the first array list
boolean unique = true;
for (int j = 0; j < totalWords2; j++) { // second loop where the
// words are being compared
if (word[i].equals(word2[j])) {
//we find two equals strings, it not unique
unique = false;
break;
}
}
//if it remains unique there wasn't equals
if (unique) {
uniqueWords.add(word[i]);
}
}
for (String s : uniqueWords) {
System.out.println(s);
}
}
}