Results 1 to 16 of 16
  1. #1
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default Comparing Two Text Files

    Hi,

    I have two text files. One has the contents of a news article and the other has a list of words that need removed from the news article. The snippet below is where I'm opening the second text file (fileTwo.txt) and attempting to compare it with "s," which is the array of characters from the first text file (this is defined earlier in the program and not shown below). I am converting "s" to strings, so that it can be compared with the strings returned from br.readLine(). Does anyone see any problems with the code below? I've used "println's" for debugging and to check to see that I'm getting the correct output from each text file and I am. It's just the comparison part that isn't working.

    Thanks.

    Java Code:
    java.io.FileReader fileReader = new java.io.FileReader("fileTwo.txt");
    BufferedReader br = new BufferedReader(fileReader);
    
    while(br.readLine() != null){
        if(br.readLine()==s.toString()){
            System.out.println("We have a match!");		
        }
    }
    br.close();

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    don't use == to compare Strings. Use either the equals(...) method or equalsIgnoreCase(...) method.

  3. #3
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    can you please add a println on both lines. just to make sure that they are really the same. And please add else statement and display something like "no match found". and since you are working with strings can you please change the "==" in your if statement to equals or equalsIgnoreCase ^_^ Please post the results
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  4. #4
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    just like fubarable said change it to equals or equalsIgnoreCase ^_^
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  5. #5
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    Wow, amazingly fast responses. Thank you! I'll report back with the results.

  6. #6
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    Thanks, that helped, but not all of the words are getting removed. Right now, the while loop goes until it gets to the end of the file and there are no more words (i.e. Null). Could the commas, periods, etc. be causing issues?

  7. #7
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    I've added some new code below, in which I'm trying to remove the list of words in file1 from file2, and eventually write it to a new text file (the code for that isn't shown below). I'm trying to break the text apart with StreamTokenizers and then compare, but I'm having some issues. Any help would be greatly appreciated.

    Thanks

    Java Code:
    import java.io.*; 
    import java.util.*;
    
    class Tokenizer {
    
        public static void main(String[] args) throws IOException {
            
            File RemoveWords = new File("file1.txt");
            File Text = new File("file2.txt");
    
            FileReader removeWordsFile = new FileReader("file1.txt");
            FileReader textFile = new FileReader("file2.txt");
           
            StreamTokenizer streamRemoveWordsTokenizer = new StreamTokenizer(removeWordsFile);
    	StreamTokenizer streamTextFileTokenizer = new StreamTokenizer(textFile);
            
            int i = 0;
    	int j = 0;
     
            int numberOfTokensGenerated = 0;
            while (j != streamTextFileTokenizer.TT_EOF) {
               while(i != streamRemoveWordsTokenizer.TT_EOF){
                      if(streamTextFileTokenizer==RemoveWordsTokenizer){
                           System.out.println("match!!!!!!!!!");
                      }
                      i = streamRemoveWordsTokenizer.nextToken();
                }
                numberOfTokensGenerated++;
            }
            System.out.println("Number of tokens = " + numberOfTokensGenerated);
        }
    }
    Last edited by coder09; 02-01-2009 at 07:06 PM. Reason: Updated Code

  8. #8
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    Code updated above. Thanks

  9. #9
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    Right now, I can get it to list the tokens from both text files and I'm trying to compare each token in file2 to file1, so that it can be removed. However, after one run-through of the tokens in file1, it just lists EOF every time.

    Thanks
    Last edited by coder09; 02-01-2009 at 07:03 PM.

  10. #10
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Java Code:
    while (j != streamTextFileTokenizer.TT_EOF) {
    StreamTokenizer streamRemoveWordsTokenizer = new StreamTokenizer(removeWordsFile);
               while(i != streamRemoveWordsTokenizer.TT_EOF){
                      if(streamTextFileTokenizer==RemoveWordsTokenizer){
                           System.out.println("match!!!!!!!!!");
                      }
                      i = streamRemoveWordsTokenizer.nextToken();
                }
                numberOfTokensGenerated++;
            }
    Try Please. . .^_^ i think all you need is a reset
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  11. #11
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    Thanks, but I get an error that states that it is already defined in main...

    Quote Originally Posted by Eku View Post
    Java Code:
    while (j != streamTextFileTokenizer.TT_EOF) {
    StreamTokenizer streamRemoveWordsTokenizer = new StreamTokenizer(removeWordsFile);
               while(i != streamRemoveWordsTokenizer.TT_EOF){
                      if(streamTextFileTokenizer==RemoveWordsTokenizer){
                           System.out.println("match!!!!!!!!!");
                      }
                      i = streamRemoveWordsTokenizer.nextToken();
                }
                numberOfTokensGenerated++;
            }
    Try Please. . .^_^ i think all you need is a reset

  12. #12
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    Removed "StreamTokenizer" and the code ran. Although, now, EOF is replaced with NOTHING.

    Thanks

  13. #13
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    Does anyone have any ideas? I placed debugging code to print the output because the logic looks correct to me (although the output is telling me otherwise).

    Thanks

  14. #14
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default

    I'm really lost with this. I've traced through the code and now that the streamtokenizer is getting reset to [NOTHING], it should proceed as it does through the first iteration of the loop (which works correctly). "i" is set back to 0 and streamtokenizer is set to what it was for the first iteration of the loop. There must be a stupid error that I'm not spotting. Please help!

    Thanks

  15. #15
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default This is working

    try
    {
    Boolean matchFlag = false;
    FileReader refFile = new FileReader(file1);
    FileReader outputFile = new FileReader(file2);
    StreamTokenizer refStreamTokenizer = new StreamTokenizer(refFile);
    StreamTokenizer outputFileStreamTokenizer = new StreamTokenizer(outputFile);
    int i = refStreamTokenizer.nextToken();
    int j = outputFileStreamTokenizer.nextToken();
    for (; (i != refStreamTokenizer.TT_EOF)&&(j != outputFileStreamTokenizer.TT_EOF); i = refStreamTokenizer.nextToken(),
    j = outputFileStreamTokenizer.nextToken())
    {
    if(i==j){
    matchFlag = true;
    }
    else
    {
    matchFlag = false;
    break;
    }
    }
    if(matchFlag)
    {
    System.out.println(file1 + " and "+ file2 + " Matched !!! ");
    }
    else
    {
    System.out.println(file1 + " and "+ file2 + " Not Matched !!! ");
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }

  16. #16
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default This is working.. I know I am very late.. But anyway this might be useful...

    try
    {
    Boolean matchFlag = false;
    FileReader refFile = new FileReader(file1);
    FileReader outputFile = new FileReader(file2);
    StreamTokenizer refStreamTokenizer = new StreamTokenizer(refFile);
    StreamTokenizer outputFileStreamTokenizer = new StreamTokenizer(outputFile);
    int i = refStreamTokenizer.nextToken();
    int j = outputFileStreamTokenizer.nextToken();
    for (; (i != refStreamTokenizer.TT_EOF)&&(j != outputFileStreamTokenizer.TT_EOF); i = refStreamTokenizer.nextToken(),
    j = outputFileStreamTokenizer.nextToken())
    {
    if(i==j){
    matchFlag = true;
    }
    else
    {
    matchFlag = false;
    break;
    }
    }
    if(matchFlag)
    {
    System.out.println(file1 + " and "+ file2 + " Matched !!! ");
    }
    else
    {
    System.out.println(file1 + " and "+ file2 + " Not Matched !!! ");
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }

Similar Threads

  1. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 03:15 AM
  2. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  3. Does OS intervene when reading Java text files
    By Tina G in forum Advanced Java
    Replies: 1
    Last Post: 04-07-2008, 02:29 PM
  4. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •