Search code examples
javaarraysword-count

Word count from text


This is my code to work out the length of a word:

public class WordCount {
public static void main (String args []) {
    String text;
    text = "Java";
    System.out.println (text);

    //Work out the length
            String [] input = text.split(" ");
            int MaxWordLength = 0;
            int WordLength = 0;
                for (int i = 0; i < input.length; i++)
                {  
                    MaxWordLength = input[i].length();
                    WordLength = MaxWordLength;                         
                } //End of working out length

                //Work out no. of words
                int[] intWordCount = new int[WordLength + 1];
                for(int i = 0; i < input.length; i++) {
                    intWordCount[input[i].length()]++; }

                for (int i = 1; i < intWordCount.length; i++) {
                    System.out.println("There are " + intWordCount[i] + " words of length " + MaxWordLength);
                }
        }
    }

The problem I am having is that when it prints out the length of the word, I get these results:

Java
There are 0 words of length 4
There are 0 words of length 4
There are 0 words of length 4
There are 1 words of length 4

But when I change the text to "J" this prints out:

J
There are 1 words of length 1

Any idea why it's doing that? P.S. I'm kind of new to Java and any help would be appreciated.


Solution

  • I am not sure if you want to count letter or word because your code counts letter to me.

    Just you need to change this line from

    String [] input = text.split(" ");
    

    to

    String [] input = text.split("");
    

    and your program works perfectly.

    input: Java
    
    output: There are 4 letters of length 1 <- Hope this is the expected result for you
    

    Source: Splitting words into letters in Java

    You can achieve this in better and less headache by using Lambda in Java

    Code:

    import java.util.*;
    
    public class LambdaTest     
    {
        public static void main (String[] args)
        {
    
             String[] st = "Hello".split("");
             Collection myList = Arrays.asList(st);
             System.out.println("your word has " + myList.stream().count() + "letters");
    
        } 
    }
    

    Output:

    your word has 5 letters CLEARLY in length 1

    My answer when you cleared what your issue is

    Code:

    public class WordCount      
    {
        public static void main (String[] args)
        {
          String text ="";
          int wordLenght = 0;
          text = "Java is awesome for Me";
          System.out.println (text);
    
    
                String [] input = text.split(" ");
                List<Integer> list = new ArrayList<>();
    
                for (int i = 0; i < input.length; i++)
                    {  
                        list.add(input[i].length());
    
                    } 
                Set<Integer> unique = new HashSet<Integer>(list);
    
               for (Integer length : unique) {
                   System.out.println("There are " + Collections.frequency(list, length) + " words of length " + length);
    }
        } 
    }
    

    output:

      There are 2 words of length 2
      There are 1 words of length 3 
      There are 1 words of length 4 
      There are 1 words of length 7
    

    Note: Read about HashSet and Set in Java

    Source: http://javarevisited.blogspot.com/2012/06/hashset-in-java-10-examples-programs.html