Advertisement
Guest User

wrwr

a guest
Sep 16th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package org.apache.commons.lang;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5. import org.apache.commons.lang.*;
  6.  
  7. class CountLetters {
  8.  
  9. //When a String is entered, the program prints the word with the largest occurance of a letter
  10.  
  11.     String LetterCount(String s) {
  12.  
  13.         String[] words = s.split("\\s+");
  14.  
  15.         char[] alphabet = new char[] {  '0','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  16.                                         'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
  17.                                         's', 't', 'u', 'v', 'w', 'x', 'y', 'z',};
  18.  
  19.         HashMap<String,Integer> MostLetters = new HashMap<String,Integer>();                        //creating the map to store the most occurances of any letter for each word
  20.         String currentWord = "";
  21.         int occurances = 0;
  22.         String currentChar = "";
  23.  
  24.         for (int i = 0; i <= words.length -1; i++) {                                                //for each word, the letter that occures the most needs to be found
  25.  
  26.                 currentWord = words[i];
  27.                 currentWord.toLowerCase();                                                          //chars are case sensitive
  28.  
  29.                 for (int y = 1; y <= alphabet.length -1; y++) {
  30.  
  31.                     currentChar = Character.toString(alphabet[y]);
  32.  
  33.                     if( StringUtils.countMatches(currentWord, currentChar) > occurances) {          //occurances will store the maximum occurance of a single letter in the string
  34.  
  35.                         occurances = StringUtils.countMatches(currentWord, currentChar);
  36.                     }
  37.  
  38.                 }
  39.  
  40.             MostLetters.put(currentWord, occurances);
  41.             System.out.println(currentWord + ": " + occurances);
  42.             occurances = 0;
  43.  
  44.         }
  45.  
  46.         /* Iterator iterator = map.keySet().iterator();
  47.  
  48.         while (iterator.hasNext()) {
  49.             String key = iterator.next().toString();
  50.             String value = map.get(key).toString();
  51.  
  52.             System.out.pringln(key + " " + value);
  53.         }*/
  54.  
  55.  
  56.         //compare words
  57.         //return correct word
  58.  
  59.  
  60.  
  61.  
  62.  
  63.     //things to add
  64.     //need to check that the String input only contains letters
  65.     //The output can include more detail about what character is the greatest, and how many
  66.         //times it occurs
  67.  
  68.         return currentWord;
  69.     }
  70.  
  71.  
  72.     public static void main(String[] args) {
  73.  
  74.     Scanner sc = new Scanner(System.in);
  75.     CountLetters c = new CountLetters();
  76.     System.out.print("Letter counter - enter a Sentence: ");
  77.     String s = sc.nextLine();
  78.     c.LetterCount(s);
  79.  
  80.  
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement