import java.util.*;
import java.io.*;
public class CheckFile<E extends Comparable<? super E>>{
SortedList<Integer> list = new SortedList<Integer>();
SortedList<Integer> noDuplicates = new SortedList<Integer>(false);
/* A list which doesn't allow for duplicates... Easier to check how many different elements there is */
/* This could of course be done with a counter, but it complicates the flow of the program somewhat, and it's only O(1)... */
public CheckFile() {
checkTheFile();
}
public void checkTheFile() {
/* Get the file... */
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("fil.txt"));
} catch (FileNotFoundException e){
System.out.println("Error opening the file");
return;
}
/* Parse the file */
try {
String oneLine = in.readLine();
while(oneLine != null) {
StringTokenizer str = new StringTokenizer(oneLine);
while(str.hasMoreTokens()) {
try {
int i = Integer.parseInt(str.nextToken());
list.insert(i); // Normal list, the one we're going to use
noDuplicates.insert(i);
} catch (NumberFormatException e) {
System.out.println("Error parsing contents of file");
return;
}
}
oneLine = in.readLine();
}
} catch (IOException e) { /* Boom */ }
/* Standard print outs... */
System.out.println("Det fanns " + noDuplicates.size() + " olika tal i filen.");
System.out.println("Nedan anges talen samt antal förekomster inom parantes");
System.out.println();
/* Go through the list, on each element, check how many other exact same elements there is... */
/* Of course, remember to remove the element... */
/* Do always it atleast once for each element... */
Iterator it = list.iterator();
while(it.hasNext()) {
int inc = 0; // Our incrementor...
Object talet = it.next(); // Element
do {
inc++;
it.remove();
} while(it.hasNext() && it.next().equals(talet));
System.out.println(talet + " (" + inc + ')'); // Printing...
it = list.iterator(); // Restore pointer...
}
}
public static void main(String[] args) {
new CheckFile<Integer>();
}
}