(String[] args)
| 15 | } |
| 16 | |
| 17 | public static void main(String[] args) |
| 18 | throws IOException |
| 19 | { |
| 20 | HashMap map = new HashMap(); |
| 21 | Pattern charsOnly = Pattern.compile("\\p{Lower}+"); |
| 22 | |
| 23 | BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); |
| 24 | String line; |
| 25 | while ((line = r.readLine()) != null) { |
| 26 | Matcher matcher = charsOnly.matcher(line.toLowerCase()); |
| 27 | while (matcher.find()) { |
| 28 | String token = matcher.group(); |
| 29 | Counter c = (Counter)map.get(token); |
| 30 | if (c != null) |
| 31 | c.count++; |
| 32 | else |
| 33 | map.put(token, new Counter()); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | ArrayList list = new ArrayList(map.entrySet()); |
| 38 | Collections.sort(list, new Comparator() { |
| 39 | public int compare(Object o1, Object o2) { |
| 40 | int c = ((Counter)((Map.Entry)o2).getValue()).count - ((Counter)((Map.Entry)o1).getValue()).count; |
| 41 | if (c == 0) { |
| 42 | c = ((String)((Map.Entry)o2).getKey()).compareTo((String)((Map.Entry)o1).getKey()); |
| 43 | } |
| 44 | return c; |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | String[] padding = { "error!", " ", " ", " ", " ", " ", " ", "error!" }; |
| 49 | StringBuffer output = new StringBuffer(); |
| 50 | Iterator it = list.iterator(); |
| 51 | while (it.hasNext()) { |
| 52 | Map.Entry entry = (Map.Entry)it.next(); |
| 53 | String word = (String)entry.getKey(); |
| 54 | String count = String.valueOf(((Counter)entry.getValue()).count); |
| 55 | if (count.length() < 7) |
| 56 | System.out.println(padding[7 - count.length()] + count + " " +word); |
| 57 | else |
| 58 | System.out.println(count + " " +word); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 |
nothing calls this directly
no test coverage detected