import java.io.File; import java.util.Scanner; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JScrollPane; public class SingleLinkedListExerciseSolution { static class Node { T data; Node next; Node() {} Node(T data) { this.data = data; } // end Node(T data, Node next) { this.data = data; this.next = next; } // end public String toString() { return data + ""; } // end toString } // end Node public SingleLinkedListExerciseSolution() { Node header = new Node(); try { Scanner fileScanner = new Scanner(new File("src/SingleLinkedListExercise.java")); fileScanner.useDelimiter("[^a-zA-Z]+"); // read and store in the list all unique words from the file while( fileScanner.hasNext() ) { String aWord = fileScanner.next(); Node p = header; // go through the list until the end or a match of aWord is found while( p.next != null ) { if( p.next.data.equals(aWord) ) break; p = p.next; }// end while // Now either p.next == null or aWord is already on the list // if p.next == null then add it to the list if( p.next == null ) p.next = new Node(aWord, p.next); } // end while } catch(Exception e) { e.printStackTrace();; } // end try/catch JTextArea outArea = new JTextArea(listToString(header),30,40); JOptionPane.showMessageDialog(null,new JScrollPane(outArea)); } // end constructor String listToString(Node header) { String out = ""; Node p = header; while( p.next != null ) { out += p.next + "\n"; p = p.next; } // end while return out; } // end listToString public static void main(String[] args) { new SingleLinkedListExerciseSolution(); System.exit(0); } // end main } // end SingleLinkedListExerciseSolution