Solved

Compare lines

Posted on 2004-10-04
4
180 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jsp insert to database example 2 59
Which non-HTML GUI front end to use with Java? 3 51
java example issue 5 42
Eclipse for Java EE development 2 43
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

740 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