Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Comparing two files and printing out matching words

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Comparing two files and printing out matching words

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

    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.

Similar Threads

  1. Printing Files using DocPrintJob
    By inxis in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 5th, 2010, 05:54 AM
  2. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  3. What is the matching mutator method?
    By ssmith in forum Object Oriented Programming
    Replies: 1
    Last Post: November 3rd, 2009, 10:03 PM
  4. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM