Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class WordFrequency{
- public static void main(String [] args){
- HashMap<Integer, Integer> recorder = new HashMap<>();
- System.out.println("Please enter word for the frequency recorder: ");
- while(!Console.endOfFile()){ //takes input until user is finished
- String userInput = Console.readToken();
- int wordLength = userInput.length();
- if ( recorder.containsKey(wordLength) ){ //checks if key is recorded
- int counter = recorder.get(wordLength);
- counter++;
- recorder.put(wordLength, counter );
- }
- else recorder.put(wordLength, 1); //otherwise record key and set count to 1
- System.out.println("Please enter word for the frequency recorder: ");
- }
- // print out the word lengths and their frequency
- Set<Integer> keys = recorder.keySet();
- for(Integer wordLength : keys){
- System.out.println("Word Length: " + wordLength + " Frequency: " + recorder.get(wordLength));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment