Results 1 to 16 of 16
Thread: Comparing Two Text Files
- 01-29-2009, 02:20 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
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();
-
don't use == to compare Strings. Use either the equals(...) method or equalsIgnoreCase(...) method.
- 01-29-2009, 02:25 AM #3
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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.
- 01-29-2009, 02:26 AM #4
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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.
- 01-29-2009, 02:53 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
Wow, amazingly fast responses. Thank you! I'll report back with the results.
- 01-31-2009, 04:47 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
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?
- 02-01-2009, 12:12 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
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
- 02-01-2009, 03:21 AM #8
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
Code updated above. Thanks
- 02-01-2009, 05:45 PM #9
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
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.
ThanksLast edited by coder09; 02-01-2009 at 07:03 PM.
- 02-02-2009, 01:23 AM #10
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Try Please. . .^_^ i think all you need is a resetJava 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++; }Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 02-02-2009, 02:14 AM #11
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
- 02-02-2009, 02:25 AM #12
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
Removed "StreamTokenizer" and the code ran. Although, now, EOF is replaced with NOTHING.
Thanks
- 02-03-2009, 12:02 AM #13
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
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
- 02-03-2009, 01:55 AM #14
Member
- Join Date
- Jan 2009
- Posts
- 20
- Rep Power
- 0
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
- 03-03-2009, 06:11 AM #15
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
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();
}
- 03-03-2009, 06:11 AM #16
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
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
-
Reading and Writing Text Files
By kandt in forum New To JavaReplies: 1Last Post: 11-12-2008, 03:15 AM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM -
Does OS intervene when reading Java text files
By Tina G in forum Advanced JavaReplies: 1Last Post: 04-07-2008, 02:29 PM -
Text and image files within jar files
By erhart in forum Advanced JavaReplies: 8Last Post: 01-19-2008, 04:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks