I did wrote a code that gets a text file as input, then the program makes to equal String arrays with the text so the words can be compared and find + count the unique and repeated words. The program becomes compiled successfully but if I try to execute it so I get the problem: Exception in thread"main"java.lang.NullPointerException...java:52. The problem must be in the way I declare the strings. How should I write them instead? Thanks!
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);
}
}
}