Program to compares two text files!
I have to make a program that compares two given text files and then tells the user if there is or isn't any differences. Then if there is, print the line number and the two lines. I got most of it but I couldn't complete the readFrom method and I wasn't sure what I was doing wrong. My teacher gave me help with it and said to fill in the blank, but I'm clueless at this point. And until I get that method done, I can't really test the whole program. So if you guys could help it'd be great. Also, it's an intro course so try to keep it simple lol thanks :)
Code :
import java.util.*;
import java.io.*;
//This program compares 2 text files and checks for differences in the two
public class CompareFiles {
//This method explains the program, asks for the file names, and calls the compareFiles method
public static void main(String[] args) throws FileNotFoundException {
System.out.println("This program compares two files for differences"); // outputs program description
System.out.println();
Scanner console = new Scanner(System.in);
System.out.println("Enter a first file name"); //asks for first file name
String name1 = console.nextLine(); //saves file as name1
System.out.println("Enter a second file name"); //asks for second file name
String name2 = console.nextLine(); //saves as name2
System.out.println();
Scanner infile1 = new Scanner(new File(name1)); //reads the text
Scanner infile2 = new Scanner(new File(name2));
compareFiles(infile1, infile2); //calls the compareFiles method
}
//this method compares the two files
public static void compareFiles(Scanner infile1, Scanner infile2) {
boolean different = false; //sets a new boolean variable to false
int line = 0; //sets a line variable to 0
while (infile1.hasNextLine() || infile2.hasNextLine()) { // if both files have a next line
String line1 = readFrom(infile1);
String line2 = readFrom(infile2);
line++;
if (!line1.equals(line2)) { //if line 1 does not equal line 2
if (!different) { // if different variable is false
System.out.println("Differences found"); //tell user there is a difference in the files
different = false; //set different to false
}
System.out.println("Line " + line + ":"); //output line number
printLine("<", line1); //calls the printLine method
printLine(">", line2);
System.out.println(); //blank line
}
if (line1.equals(line2)) { //if line 1 equals line 2
System.out.println("No differences found"); //no differences found
}
}
}
//returns a line of text
public static String readFrom(Scanner infile) {
if (infile.hasNextLine()) {
____ infile.nextLine();
}
else {
return null;
}
}
//prints the given line or says it is the end of the file
public static void printLine(String prefix, String line) {
System.out.print(prefix + " ");
if (line == null) { //if there are no more lines
System.out.println("no such line (end-of-file)"); //tell the user
}
else {
System.out.println(line); //if there is a line, print the line
}
}
}
Re: Program to compares two text files!
You should never compare strings with == or != unless you are checking against null.
To compare strings:
Wrap your code in highlight/code tags! [highlight=Java]**Java Code goes here[/highlight] [code]**Code goes here**[/code] either one works
Re: Program to compares two text files!
Ok I fixed it. Thanks for the tip. But in the readFrom method I need to complete the code where the blank is and that's what is stumping me. I think if there is a next line to be read, I need to read it, then return the result back through the parameter. But I don't know how to do this lol
Re: Program to compares two text files!
I know I am 2 years late to answer here..but you could use a BufferedLineReader to get the scanner to get the line number...