Solved

Compare lines

Posted on 2004-10-04
4
153 Views
Last Modified: 2010-03-31
How can i compare a line of a file with every line of another file?
0
Comment
Question by:mmartha
  • 2
4 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 12222676
BufferedReader in1 = new BufferedReader(new FileReader(file1));
BufferedReader in2 = new BufferedReader(new FileReader(file2));
String line1 = null;
String line2 = null;
while ((line1=in1.readLine())!=null && (line2=in2.readLine())!=null)
{
   if (line1.equals(line2))
   {
      System.out.println("match");
   }
   else
   {
      System.out.println("mismatch");
   }
}
in1.close();
in2.close();
0
 
LVL 37

Accepted Solution

by:
zzynx earned 400 total points
ID: 12224380
That compares each line of file1 with the corresponding line of file2.
The question was: "How can i compare a line of a file with *every line* of another file?

What line of the first file do you want to compare with every line of a second file?

final int NR_OF_LINE = 5;  // assuming you want to compare the 5th line of file1 with every line in file2:

BufferedReader in1 = new BufferedReader(new FileReader(file1));
BufferedReader in2 = new BufferedReader(new FileReader(file2));
String line1 = null;
String line2 = null;
int lineNr = 0;
// Get the 5th line of file1:
while ( lineNr!=NR_OF_LINE && (line1==in1.readLine())!=null )
  lineNr++;
in1.close();

// compare that line which every line of file2
while ( (line2=in2.readLine())!=null ) {
   if (line1.equals(line2)) {
      System.out.println("match");
   }
   else {
      System.out.println("mismatch");
   }
}
in2.close();
0
 
LVL 2

Author Comment

by:mmartha
ID: 12278581
zzynx,
your answer have some errors, but give me the idea.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284307
Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 52
What is JNDI datasource in spring 1 39
topping3 challenge 14 52
java set up 1 48
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now