I'm trying to read in a list of words a file using BufferedReader
and determine if any are duplicates, and if they are, to not print them. I know that there is a SuperClass called "UniqueLineReader" that can check the occurrence of duplicate lines for you, and something else called Linked HashSet
. However, I haven't learned either of these in class, so I'm trying not to use them.
Anyways, the e.getMessage() is printing "null", which I don't understand, because the file obviously isn't and I'm checking that it isn't. Do I have wrong placement of variables?
try {
inFile = new BufferedReader(new FileReader(inputName));
outFile = new PrintWriter(new FileWriter(outputName));
while((nextLine = inFile.readLine())!= null){
if(indexOf(nextLine) == -1){
insert(nextLine);
outFile.println(nextLine);}
else{
System.out.println(nextLine + " has been seen before!");
}
}
inFile.close();
}//try
catch(Exception e){
System.out.println(e.getMessage());
}//catch
My indexOf
method:
private int indexOf(String newWord){
for(int i = 0; i < numWords; i++){
if(arrayStrings[i].equals(newWord)){
return i;
}
}
return -1;
}//indexOf
stacktrace
`java.lang.NullPointerException at A2Q2.indexOf(A2Q2.java:58) at A2Q2.removeDuplicateLines(A2Q2.java:79) at TestA2Q2.main(TestA2Q2.java:10)