import java.awt.Font; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.util.Comparator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** * * @author Bill Kraynek */ public class CountWordsWithWordClassUsingMaps { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { URL url = new URL("http://www.cis.fiu.edu/~kraynek/COP3337-examples/CountWordsWithWordClassUsingMaps.java"); URLConnection connection = url.openConnection(); Scanner fileScanner = new Scanner(connection.getInputStream()); fileScanner.useDelimiter("[^A-Za-z]+"); Map words = new TreeMap<>(new IgnoreCaseComparator()); //int maxWordLength = 35; while (fileScanner.hasNext()) { String word = fileScanner.next(); if (!words.keySet().contains(word)) { words.put(word, new WordClass(word)); }// end if words.get(word).incrementCount(); }// end while displayMap(words); Map words1 = new TreeMap<>(new CountWordsComparator(words)); words1.putAll(words); displayMap(words1); Set values = new TreeSet<>(words.values()); displaySet(values); Set values1 = new TreeSet<>(new CountWordsComparatorA()); values1.addAll(words.values()); displaySet(values1); Set> entries = new TreeSet<>(new CompareEntryWord()); entries.addAll(words.entrySet()); displayEntry(entries); entries = new TreeSet<>(new CompareEntryCount()); entries.addAll(words.entrySet()); displayEntry(entries); String[] allWords = words.keySet().toArray(new String[0]); while (true) { String aWord = (String) JOptionPane.showInputDialog(null, "Choose a word", "All words in file", JOptionPane.PLAIN_MESSAGE, null, allWords, allWords[0]); if (aWord == null) { return; }// end if WordClass word = words.get(aWord); JOptionPane.showMessageDialog(null, word.getWord() + " appeared " + word.getCount() + " time(s)."); }// end while } static void displaySet(Set words) { String blanks = ""; for (int i = 0; i < WordClass.maxWordLength + 1; i++) { blanks += " "; } String out = " Word" + blanks.substring(8) + " Count\n\n"; for (WordClass word : words) { out += word; }// end for JTextArea outArea = new JTextArea(out, 30, 40); outArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 15)); JOptionPane.showMessageDialog(null, new JScrollPane(outArea)); } static void displayEntry(Set> entries) { String blanks = ""; for (int i = 0; i < WordClass.maxWordLength + 1; i++) { blanks += " "; } String out = " Word" + blanks.substring(8) + " Count\n\n"; for (Entry entry : entries) { out += entry.getValue(); } // end for JTextArea outArea = new JTextArea(out, 30, 40); outArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 15)); JOptionPane.showMessageDialog(null, new JScrollPane(outArea)); } static void displayMap(Map words) { String blanks = ""; for (int i = 0; i < WordClass.maxWordLength + 1; i++) { blanks += " "; } String out = " Word" + blanks.substring(8) + " Count\n\n"; for (String word : words.keySet()) { out += words.get(word); }// end for JTextArea outArea = new JTextArea(out, 30, 40); outArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 15)); JOptionPane.showMessageDialog(null, new JScrollPane(outArea)); } static class WordClass implements Comparable { private String word; private int count; public static int maxWordLength = 0; public WordClass(String word) { this.word = word; count = 0; if (maxWordLength < word.length()) { maxWordLength = word.length(); } } public String getWord() { return word; } public int getCount() { return count; } public void incrementCount() { count++; } public boolean equals(Object rhs) { if (rhs == null) { return false; } if (!this.getClass().equals(rhs.getClass())) { return false; } WordClass that = (WordClass) rhs; return this.getWord().equals(that.getWord()); } public int hashCode() { return word.hashCode(); } public String toString() { String blanks = ""; for (int i = 0; i < maxWordLength + 1; i++) { blanks += " "; }// ed for String out = ""; int countSpaces = 3 - (count+"").length(); out += word + blanks.substring(word.length()) + blanks.substring(0, countSpaces) + count + "\n"; return out; } public int compareTo(WordClass rhs) { return this.getWord().compareTo(rhs.getWord()); } }// end WordClass static class CountWordsComparator implements Comparator { private Map words; public CountWordsComparator(Map words) { this.words = words; }// end constructor public int compare(String lhs, String rhs) { int countDifference = words.get(rhs).getCount() - words.get(lhs).getCount(); if (countDifference != 0) { return countDifference; } return lhs.compareTo(rhs); } }// end WordCountComparator static class CountWordsComparatorA implements Comparator { public int compare(WordClass lhs, WordClass rhs) { int countDifference = rhs.getCount() - lhs.getCount(); if (countDifference != 0) { return countDifference; } return lhs.compareTo(rhs); } } static class CompareEntryCount implements Comparator> { public int compare(Entry lhs, Entry rhs) { int diff = rhs.getValue().getCount() - lhs.getValue().getCount(); if( diff != 0 ) return diff; return lhs.getKey().compareTo(rhs.getKey()); } } static class IgnoreCaseComparator implements Comparator { public int compare(String lhs, String rhs) { return lhs.compareToIgnoreCase(rhs); } } static class CompareEntryWord implements Comparator> { public int compare(Entry lhs, Entry rhs) { return lhs.getKey().compareTo(rhs.getKey()); } } }