Solved

Compare lines

Posted on 2004-10-04
4
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…
Suggested Courses

632 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