count the number of words in a string java

Java
 public static void main(String[] args)
    { 
        //return the number of words in a string 
        
       String example = "This is a good exercise"; 
       
       int length = example.split(" ").length;
       
       System.out.println("The string is " + length + " words long.");
        
        
    }
Source

Also in Java: