Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Program to compares two text files!

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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
    		}
    	}
    }
    Last edited by nWeid1; December 5th, 2011 at 12:18 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Program to compares two text files!

    You should never compare strings with == or != unless you are checking against null.

    To compare strings:
    string1.equals(string2)
    !string1.equals(string2)

    Wrap your code in highlight/code tags! [highlight=Java]**Java Code goes here[/highlight] [code]**Code goes here**[/code] either one works

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

Similar Threads

  1. Need help to parse text files.
    By Num1701 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 18th, 2011, 02:11 PM
  2. Convert Text files to XML
    By HelloAll in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2011, 05:01 AM
  3. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  4. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM
  5. Merge 2 Sorted Text Files into a Third
    By Epyllion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2010, 08:24 PM