Results 1 to 7 of 7
  1. #1
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Need help completing a program to compare 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






    Java 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
    		}
    	}
    }
    Last edited by nweid1; 12-05-2011 at 07:20 PM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Need help completing a program to compare two text files!

    Use the equals method from String class to compare two Strings. Java does not use == to compare Strings
    Java Code:
    String str1 = "Foo";
    String str2 = "Bar";
    
    System.out.println(str1.equals(str2));

  3. #3
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help completing a program to compare 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
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Need help completing a program to compare two text files!

    readFrom() returns a String type. The paramater of the method i.e. infile reads in a String. Simply return the String that has been read by infile.

  5. #5
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help completing a program to compare two text files!

    Wow. I'm sooooo dumb. Thanks a lot!

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Need help completing a program to compare two text files!

    Just lacking experience, keep coding and this stuff will be second nature to you.

  7. #7
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help completing a program to compare two text files!

    Haha yeah I'm just getting into it but I'm likin' it. I just have some trouble keeping everything straight in my mind. So much code to remember haha

Similar Threads

  1. Compare 2 pdf files
    By kris_java in forum Advanced Java
    Replies: 9
    Last Post: 12-07-2012, 01:22 PM
  2. Compare 2 files
    By GTMPAL in forum New To Java
    Replies: 2
    Last Post: 10-12-2011, 07:28 PM
  3. anyone have algorithm for text compare?
    By franco in forum New To Java
    Replies: 8
    Last Post: 08-08-2011, 05:32 PM
  4. Compare 2 large files
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-23-2008, 03:18 AM
  5. Completing A Program..
    By Louise1875 in forum New To Java
    Replies: 1
    Last Post: 05-11-2008, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •