Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing two files

 
divya madala
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
My intension is to grab the common lines from 2 files in which there are lines of 6 digits with a hyphen in the middle.
like say
123-652
982-648 and so on...........
It's surprising to me that the following code prints nothing though I have many lines in common in those 2 files. Can somebody please help where I am going wrong?



Thanks,
Divya
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't think this code does what you think it does. It sure doesn't compare every line in two files. Try printing out both line and str.
[ January 30, 2004: Message edited by: Joe Ess ]
 
divya madala
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strangly, nothing prints even when I print both the lines.
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That kind of depends on your data file. You may want to add some text to your System.out.println(). Like:

Just in case you have some blank lines or something weird in the data.
 
divya madala
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Billybob Marshall
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or maybe it's not even getting into the code where you've placed the printlns, because the file doesn't exist. I notice you are using relative path names, so the execution of your app depends on being launched in a particular directory, which is almost always a no-no. You should be reading files from the classpath instead of using directory names. For one reason, suppose you decide to jar-up the code and file(s) - then they are no longer files on a physical directory.
Use:
InputStream stream = YourClass.class.getResource("/yourfile.txt")
to open from the classpath (remember to close it afterwards as well).
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by divya madala:

it prints only the first line of 1st file proir to every line of 2nd file

Any idea as to why this may be?
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by divya madala:
it prints only the first line of 1st file proir to every line of 2nd file

HINT:
Try printing out the values like this:

and make sure you label them in your System.out.println's.
 
kiranmayi Talusani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
divya,
First your program is comparing first line from first file with all the lines from second file.

After that it is comparing all the lines from the first file with the last line(after first line is compared with all the lines from second file, the value in "str" will be last line from second file) from second file.I guess that is the reason you are not seeing the currect output.
 
kiranmayi Talusani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divya,
I think your program is only comparing first line from the first file with all the lines from the second file,because the value of 'str' becomes 'null'.
when the program's execution comes out of the second(inner) while loop the value of 'str' will become 'null'. Then it will read second line from frist file.But it will not enter into the second(inner) while loop because the value in str is 'null'. so it will read all the lines from first file util the value in 'line' becomes 'null' and then it comes out of the first(outer) while loop.
 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once your loop through values contains in String str, you have to start reading same file from first.
I do not remember whether FileReader does have any method to move file pointer to a specifiled poistion.So in your case, after first traverse, you str value will always be null and hence it will never compare.
Instead you can use RandomAccessFile class which have seek() methos which
set the FILE pointer wherever you want.In addition,you does not need to use
BuffereReader with RandomAccessFile class.
You can see this example.
if (fileName.exists()) {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
RandomAccessFile raf = new RandomAccessFile(fileName1, "r");
String line="";
String str="";
line = br.readLine();
while(line != null)
{
raf.seek(0);
str = raf.readLine();
while(str != null)
{
if(line.equalsIgnoreCase(str))
{
System.out.println("MATCHED :" + line);
}
str = raf.readLine();
}
line = br.readLine();
}
}

Hope this help
[ February 02, 2004: Message edited by: himanshu patel ]
 
Joe Ess
Bartender
Posts: 9339
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiranmayi and himanshu, easy on just giving the answers away. This is obviously a school assignment so we want our friend here to learn the valuable lesson of how to debug his own code!
 
divya madala
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your answers Himanshu and Kiranmai. It helped a lot!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic