Monday, August 2, 2010

Removing duplicates

package duplicates;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class Nodups {
/**
* @Michael Gagala
* ver 1.0
* This program will read in a strings from an input file, and
* printed the strings in a sorted order with duplicates removed
* 15 Aug 2009
*/
public static void main(String[] args) {
String file = “”;
boolean done = false;
int count = 0;
int tsCount = 0;
ArrayList wds = new ArrayList ();
TreeSet ts = new TreeSet(wds);

do{
try{
file = JOptionPane.showInputDialog(“Enter the name of the file.”);
FileReader dataFile = new FileReader(file);
Scanner sc = new Scanner(dataFile);
while(sc.hasNext()){
String aLine = sc.next();
wds.add(aLine);
count++;
}
ts.addAll(wds);
tsCount = ts.size();
Iterator it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println(“\nThere were ” + count + ” words in the orginal file.”);
System.out.println(“\nThere were ” + tsCount + ” words in the sorted file.”);
done = true;
}//close try
catch (FileNotFoundException e){
System.out.println(“File not found, please re-enter.”);
}//close catch
}//close do
while(!done);
}//close main
}//close class

No comments:

Post a Comment

Chitika