Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.apache.commons.lang;
- import java.util.*;
- import java.io.*;
- import org.apache.commons.lang.*;
- class CountLetters {
- //When a String is entered, the program prints the word with the largest occurance of a letter
- String LetterCount(String s) {
- String[] words = s.split("\\s+");
- char[] alphabet = new char[] { '0','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
- 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
- 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',};
- HashMap<String,Integer> MostLetters = new HashMap<String,Integer>(); //creating the map to store the most occurances of any letter for each word
- String currentWord = "";
- int occurances = 0;
- String currentChar = "";
- for (int i = 0; i <= words.length -1; i++) { //for each word, the letter that occures the most needs to be found
- currentWord = words[i];
- currentWord.toLowerCase(); //chars are case sensitive
- for (int y = 1; y <= alphabet.length -1; y++) {
- currentChar = Character.toString(alphabet[y]);
- if( StringUtils.countMatches(currentWord, currentChar) > occurances) { //occurances will store the maximum occurance of a single letter in the string
- occurances = StringUtils.countMatches(currentWord, currentChar);
- }
- }
- MostLetters.put(currentWord, occurances);
- System.out.println(currentWord + ": " + occurances);
- occurances = 0;
- }
- /* Iterator iterator = map.keySet().iterator();
- while (iterator.hasNext()) {
- String key = iterator.next().toString();
- String value = map.get(key).toString();
- System.out.pringln(key + " " + value);
- }*/
- //compare words
- //return correct word
- //things to add
- //need to check that the String input only contains letters
- //The output can include more detail about what character is the greatest, and how many
- //times it occurs
- return currentWord;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- CountLetters c = new CountLetters();
- System.out.print("Letter counter - enter a Sentence: ");
- String s = sc.nextLine();
- c.LetterCount(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement