Comparing two files and printing out matching words
Code Java:
import java.io.*;
import java.util.Arrays;
public class Driver {
private FileReader mReader,idReader;
private BufferedReader mobyReader,myIdentifierReader;
private FileOutputStream vStream;
private PrintWriter validatedStream;
private String mLine, idLine,mLineArray[],idLineArray[];
public Driver(){
try{
int i,j,b,k=0;
mReader = new FileReader("WordListMoby.txt");
mobyReader = new BufferedReader(mReader);
idReader = new FileReader("myIdentifiers.txt");
myIdentifierReader = new BufferedReader(idReader);
vStream = new FileOutputStream("validatedIdentifiers.txt.");
validatedStream = new PrintWriter(vStream);
mLineArray = new String[100];
idLineArray = new String[100];
mLine = mobyReader.readLine();
idLine = myIdentifierReader.readLine();
for( j=0;idLine!=null;j++){
if(j==idLineArray.length){
idLineArray = Arrays.copyOf(idLineArray,idLineArray.length + 100);
}
idLineArray[j]=idLine;
idLine = myIdentifierReader.readLine();
for( i=0;mLine!=null;i++){
if(i==mLineArray.length){
mLineArray = Arrays.copyOf(mLineArray,mLineArray.length + 100);
}
mLineArray[i]=mLine;
mLine = mobyReader.readLine();
if(mLineArray[i].equals(idLineArray[j])){
System.out.println(idLineArray[j]);
}
}
} myIdentifierReader.close();
mobyReader.close();
}catch(IOException e){
System.out.println("I/O exception");
}
}
public static void main(String[] args) {
Driver d = new Driver();
}
}
What this is suppose to do is compare the words found in myIdentifiers.txt and compare them to the WordListMoby.txt file. The myIdentifiers file is all user inputted words. I currently have jumping,running,falling, and xyz in the file. The WordListMoby file has every word in the english dictionary. When I run the program it only outputs jumping (the first word that it found as a match in both files), but won't print falling or running when it should. Any suggestions. I believe the error is in the .equals loop but I have been toying with this for hours to try and get it to work with out any luck.
Re: Comparing two files and printing out matching words
Can you describe your algorithm?
Your description suggests that you don't go back to the being of the dictionary to start searching for the second word.
Quote:
I believe the error is in the .equals loop
Where is the .equals loop? You don't have any comments in the code describing what each section is supposed to do and how it is going to do it.