Anagram

Anagram - Arrays.sort(sortedList); String words = new...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* @author Tung Le * @file Anagram.java * Assignment 5 * Course: CSC 1351 * Instructor: Blanks * ID: cs135112 */ import java.io.*; import java.util.*; public class Anagram { private Map<String, Set<String>> anagramMap; private int threshold; public Anagram(BufferedReader br, int threshold) { anagramMap = new HashMap<String, Set<String>> (); this.threshold = threshold; String word, representation; try { while ((word = br.readLine()) != null) { representation = wordToRepresentation(word.toLowerCase()); if (anagramMap.containsKey(representation)){ Set<String> temp = anagramMap.get(representation); temp.add(word); anagramMap.put(representation, temp); } else{ Set<String> temp = new HashSet<String>(); temp.add(word); anagramMap.put(representation, temp); } } } catch(IOException ioe) { System.out.println("I/O error: " + ioe); } } private String wordToRepresentation(String word){ char[] sortedList = word.toCharArray();
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Arrays.sort(sortedList); String words = new String(sortedList); return words; } @Override public String toString(){ String s = &quot;&quot;; Set&lt;Map.Entry&lt;String, Set&lt;String&gt;&gt;&gt; eset = anagramMap.entrySet(); for (Map.Entry me : eset){ Set temp = (Set) me.getValue(); if(temp.size() &gt;= threshold) s+= String.format(&quot;%1s %n&quot;, temp); } return s; } public static void main(String args) { if (args.length != 2) { System.out.println(&quot;usage: java Anagram &lt;inputfile&gt; &lt;threshold&gt;&quot;); System.exit(0); } try { BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(args[0]))); Anagram a = new Anagram(br, Integer.parseInt(args[1])); System.out.println(a); } catch(FileNotFoundException fnfe) { System.err.println(&quot;Open error: &quot; + fnfe); } catch(IOException ioe) { System.err.println(&quot;I/O error: &quot; + ioe); } } }...
View Full Document

Page1 / 2

Anagram - Arrays.sort(sortedList); String words = new...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online