0

My code counts the number of words in a string declared , but i need a code that accepts a paragraph from keyboard and then print out the frequency of each word For eg ,

Enter your paragraph

My code doesnt work properly code needs to work

My-1

Code-2

Doesnt-1

Work-2

properly -1

needs-1

To-1

public class Main {

    public static void main(String[] args)
    {       
        String text = "the quick brown fox jumps fox fox over the lazy dog brown";
        String[] keys = text.split(" ");
        String[] uniqueKeys;
        int count = 0;
        System.out.println(text);
        uniqueKeys = getUniqueKeys(keys);
        
        for(String key: uniqueKeys)
        {
            if(null == key)
            {
                break;
            }           
            for(String s : keys)
            {
                if(key.equals(s))
                {
                    count++;
                }               
            }
            System.out.println("Count of ["+key+"] is : "+count);
            count=0;
        }
    }
    
    private static String[] getUniqueKeys(String[] keys)
    {
        String[] uniqueKeys = new String[keys.length];
        
        uniqueKeys[0] = keys[0];
        int uniqueKeyIndex = 1;
        boolean keyAlreadyExists = false;
        
        for(int i=1; i<keys.length ; i++)
        {
            for(int j=0; j<=uniqueKeyIndex; j++)
            {
                if(keys[i].equals(uniqueKeys[j]))
                {
                    keyAlreadyExists = true;
                }
            }           
            
            if(!keyAlreadyExists)
            {
                uniqueKeys[uniqueKeyIndex] = keys[i];
                uniqueKeyIndex++;               
            }
            keyAlreadyExists = false;
        }       
        return uniqueKeys;
    }
}
virusey
  • 1
  • 2

2 Answers2

0

Use a Scanner to get the user input (via Scanner.nextLine()), split it by spaces to get the words, and convert it to a Set to get the unique words.

Loop through the Set with an enhanced for loop, and count the occurrences of the word by looping through the array of words and checking whether the current item is equal to the word looped through in the Set:

Scanner sc = new Scanner(System.in);
String text = sc.nextLine(); //read line
String[] keys = text.split(" "); //get words
Set<String> unique = new HashSet<>(Arrays.asList(keys)); //get unique words
for(String s : unique) {
    int matches = 0;
    for(int i = 0; i < keys.length; i++) {
        if(keys[i].equals(s)) matches++;
    }
    System.out.println(s + "-"+ matches);
 }
 sc.close();

Sample Run:

the quick brown fox jumps fox fox over the lazy dog brown
over-1
quick-1
lazy-1
jumps-1
brown-2
fox-3
the-2
dog-1
Spectric
  • 30,714
  • 6
  • 20
  • 43
0

You can use the below lines of code

    String paragraph = "My code doesnt work properly code needs to work";
    List <String> listStringWords = Arrays.asList(paragraph.split(" "));
    Map <String, Integer> stringWithOccurrences = new LinkedHashMap<>();
    Set<String> uniqueElementsSet = new HashSet<>();
    for(String word : listStringWords){
        if(uniqueElementsSet.add(word))
        {
            stringWithOccurrences.put(word , 1);
        }
        else 
            stringWithOccurrences.put(word ,stringWithOccurrences.get(word)+1);
    }
    
    System.out.println(stringWithOccurrences);
Lisbon
  • 126
  • 1
  • 1
  • 12