Advertisement
Capple

Untitled

May 10th, 2020
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. class Word implements WordInterface {
  4.     private final String word;
  5.     Character t2ht;
  6.  
  7.     public Word(String word) {
  8.        
  9.         this.word = word;
  10.     }
  11.  
  12.     public HashMap<Character, Integer> charCount(Character t2ht) {
  13.         HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
  14.         char[] charArray = word.toCharArray();
  15.  
  16.         for (char c : charArray) {
  17.             if (c == t2ht) {
  18.                 if (charMap.containsKey(c)) {
  19.                     charMap.put(c, charMap.get(c) + 1);
  20.                 } else {
  21.                     charMap.put(c, 1);
  22.                 }
  23.             }
  24.  
  25.         }
  26.         System.out.println("Tähe arv sõnas " + '"' + word + '"');
  27.         charMap.forEach((k, v) -> {
  28.             System.out.println(k + ": " + v);
  29.         });
  30.         return charMap;
  31.     }
  32.  
  33.     public String toString(){
  34.         return word;
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement