participate


Java Programming - A program that compares documents
<<   Back to Forum  |   Give us Feedback
This topic has 3 replies on 1 page.
KTMBoi88
Posts:2
Registered: 11/3/09
A program that compares documents   
Nov 3, 2009 3:33 PM

 
Hello all,

Let me begin by saying this is not for homework or extra credit, this is a program that my instructor talked about in class and suggested we try purely as an exercise of our skills.

That said, I have made several iterations of this and I believe this is the best version however I am unable produce any output. I have invested some amount of time at this point and need some help.

The idea of this program is to accept two files as inputs. The program should then compare the two files and if it finds any differences between the files (except for spaces and capitalization) the program should return the line numbers of the files and the text from each line. With that being said, here is what I have so far.

import java.io.File;
import java.io.IOException;
 
public class TextCompare {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		
		File file1 = new File ("Bill-of-Rights.txt");
		File file2 = new File ("Bill-of-Rights-new.txt");
		
		FileCompare.compare(file1,file2);
	}
}


TextCompare imports the files and passes them to FileCompare:

import java.io.File;
import java.io.IOException;
 
public class FileCompare {
 
	public static void compare(File inptFile1, File inptFile2)
	throws IOException {
 
		CompareFile file1 = new CompareFile(inptFile1);
		CompareFile file2 = new CompareFile(inptFile2);
 
		do {
			file1.nextLine();
			file2.nextLine();
 
			file1.removeSpace();
			file2.removeSpace();
 
			file1.compare(file2);
		}
		while (file1.hasNxtLine() && file2.hasNxtLine());
	}
}


FileCompare executes a high-level comparison of the files calling methods from CompareFile

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
 
public class CompareFile {
 
	private File file;
	private int lineNumber;
	private String line;
	private int pos;
	Scanner scan;
 
	public CompareFile(File file) {
		this.file = file;
		lineNumber = 0;
	}
 
	public String nextLine() throws IOException {
		scan = new Scanner(file);
		line = scan.nextLine();
		while (isBlank()) {
			line = scan.nextLine();
			lineNumber++;
		}
		return line;
	}
 
	public String removeSpace() {
		return (line.replace(" ",""));
	}
	
	public void compare(CompareFile file2) {
		if (line.compareToIgnoreCase(file2.getLine()) != 0)
			System.out.println (lineNumber + ": " + line + "\n" +
					file2.getLineNum() + ": " + file2.getLine());
		return;
	}
 
	private boolean isBlank() {
		pos = 0;
		while (line.charAt(pos) == ' ' || line.charAt(pos) == '\n' || line.charAt(pos) == '\t') {
			pos++;
		}
		if (line.charAt(pos) < line.length())
			return false;
		else
			return true;
	}
	
	public String getLine() {
		return line;
	}
	
	public int getLineNum() {
		return lineNumber;
	}
	
	public boolean hasNxtLine() {
		return scan.hasNextLine();
	}
	
}


CompareFile is the crux of the program as you can see.

I'm sorry that the class and variable names are terrible but I just finished this and I am at my wits end because I have been trying to make this run for so long.

The file compiles just fine however when I run it the program hangs and does not produce any output. In class we have just covered loops and conditionals, but I am not familiar with anything more complex such as array operations.

Your help is greatly appreciated.

-Will
 
amp88
Posts:87
Registered: 12/22/08
Re: A program that compares documents   
Nov 3, 2009 3:35 PM (reply 1 of 3)  (In reply to original post )

 
Haven't fully looked through the code yet but could you provide your text files to us so we can compare results?
 
DrClap
Posts:38,751
Registered: 4/30/99
Re: A program that compares documents   
Nov 3, 2009 3:42 PM (reply 2 of 3)  (In reply to original post )

 
I don't think that creating a new Scanner to read each line of the file is the right thing to do. More likely to repeatedly read the first line of the file, I would have thought.
 
KTMBoi88
Posts:2
Registered: 11/3/09
Re: A program that compares documents   
Nov 3, 2009 5:42 PM (reply 3 of 3)  (In reply to #2 )

 
Good point Dr.Clap I didn't even think about the ramifications of that. If I create a single scanner in the constructor will it continue to track my current line as I jump between methods? I tweaked my program so that I create the scanner object in the constructor. Now I'm getting an index out of range error. Any ideas?

Here are copies of the text files I was attempting to scan. This came from the poly.sci department of the school and shows how removing several words from the bill of rights turns the country into a dictatorship. Please ignore the politics it was meant to be lighthearted.

Original document:
The United States Bill of Rights.
 
The Ten Original Amendments to the Constitution of the United States
Passed by Congress September 25, 1789
Ratified December 15, 1791
 
 
 
I
 
Congress shall make no law respecting an establishment of religion,
or prohibiting the free exercise thereof; or abridging the freedom of speech,
or of the press, or the right of the people peaceably to assemble,
and to petition the Government for a redress of grievances.
 
 
II
 
A well-regulated militia, being necessary to the security of a free State,
the right of the people to keep and bear arms, shall not be infringed.
 
 
III
No soldier shall, in time of peace be quartered in any house,
without the consent of the owner, nor in time of war,
but in a manner to be prescribed by law.
 
 
IV
 
The right of the people to be secure in their persons, houses, papers,
and effects, against unreasonable searches and seizures, shall not be violated,
and no Warrants shall issue, but upon probable cause, supported by oath
or affirmation, and particularly describing the place to be searched,
and the persons or things to be seized.
 
 
V
 
No person shall be held to answer for a capital, or otherwise infamous crime,
unless on a presentment or indictment of a Grand Jury, except in cases arising
in the land or naval forces, or in the Militia, when in actual service
in time of War or public danger; nor shall any person be subject for
the same offense to be twice put in jeopardy of life or limb;
nor shall be compelled in any criminal case to be a witness against himself,
nor be deprived of life, liberty, or property, without due process of law;
nor shall private property be taken for public use without just compensation.
 
 
VI
 
In all criminal prosecutions, the accused shall enjoy the right to a
speedy and public trial, by an impartial jury of the State and district
wherein the crime shall have been committed, which district shall have
been previously ascertained by law, and to be informed of the nature
and cause of the accusation; to be confronted with the witnesses against him;
to have compulsory process for obtaining witnesses in his favor,
and to have the assistance of counsel for his defense.
 
 
VII
 
In suits at common law, where the value in controversy shall exceed
twenty dollars, the right of trial by jury shall be preserved,
and no fact tried by a jury shall be otherwise re-examined in any court
of the United States, than according to the rules of the common law.
 
 
VIII
 
Excessive bail shall not be required nor excessive fines imposed,
nor cruel and unusual punishments inflicted.
 
 
IX
 
The enumeration in the Constitution, of certain rights,
shall not be construed to deny or disparage others retained by the people.
  
X
 
The powers not delegated to the United States by the Constitution,
nor prohibited by it to the States, are reserved to the States respectively,
or to the people.





Modified version:

The United States Bill of Rights.
 
The Ten Original Amendments to the Constitution of the United States
Passed by Congress September 25, 1789
Ratified December 15, 1791
 
 
 
I
 
Congress shall make no law respecting an establishment of religion,
or prohibiting the free exercise thereof; or abridging the freedom of speech,
or of the press, or the right of the people peaceably to assemble,
 
 
II
 
A well-regulated militia, being necessary to the security of a free State,
the right of the people to keep and bear arms, shall not be infringed.
 
 
III
No soldier shall, in time of peace be quartered in any house,
without the consent of the owner, nor in time of war,
but in a manner to be prescribed by law.
 
 
IV
 
The right of the people to be secure in their persons, papers,
and effects, against unreasonable searches and seizures, shall not be violated,
and no Warrants shall issue, but upon probable cause, supported by oath
or affirmation, and particularly describing the place to be searched,
and the persons or things to be seized.
 
 
V
 
No person shall be held to answer for a capital, or otherwise infamous crime,
unless on a presentment or indictment of a Grand Jury, except in cases arising
in the land or naval forces, or in the Militia, when in actual service
in time of War or public danger; nor shall any person be subject for
the same offense to be twice put in jeopardy of life or limb;
nor shall be compelled in any criminal case to be a witness against himself,
nor be deprived of life, liberty, or property, without due process of law;
nor shall private property be taken for public use without just compensation.
 
 
VI
 
In all criminal prosecutions, the accused shall not enjoy the right to a
speedy and public trial, by an impartial jury of the State and district
wherein the crime shall have been committed, which district shall have
been previously ascertained by law, and to be informed of the nature
and cause of the accusation; to be confronted with the witnesses against him;
to have compulsory process for obtaining witnesses in his favor,
and to have the assistance of counsel for his defense.
 
 
VII
 
In suits at common law, where the value in controversy shall exceed
twenty dollars, the right of trial by jury shall be preserved,
and no fact tried by a jury shall be otherwise re-examined in any court
of the United States, than according to the rules of the common law.
 
 
VIII
 
Excessive bail shall not be required nor excessive fines imposed,
nor cruel and unusual punishments inflicted.
 
 
IX
 
The enumeration in the Constitution, of certain rights,
shall not be construed to deny or disparage others retained by the people.
  
X
 
The powers not delegated to the United States by the Constitution,
nor prohibited by it to the States, are reserved to the States respectively,


Edited by: KTMBoi88 on Nov 3, 2009 4:53 PM

Edited by: KTMBoi88 on Nov 3, 2009 5:37 PM
 
This topic has 3 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums