Question

So, I've got a java assignment in which I have a String with a phrase. I need to count each word the phrase has and then count how many letters each word has. I've been able to split the phrase into words by using the Tokenizer, and then count and print the amount of words with .countTokens(). However, I'm not being able to count the letters in each word.

Basically the output should be something like this:

"Nihil veritas est"
Words: 3
Nihil: 5 letters
Veritas: 7 letters
Est: 3 letters

Here's my code so far:

public class words {
public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.println("Please type a phrase.");
    String phrase= in.nextLine();
    StringTokenizer stoken = new StringTokenizer(phrase);
    System.out.println("Your phrase has "+stoken.countTokens()+"words");


}
Was it helpful?

Solution

Try this:

public class words {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Please type a phrase.");
        String phrase= in.nextLine();

        String[] words = phrase.split(" ");

        System.out.println("The number of words is:"+words.length);

        for(int i=0; i<words.length; i++){
            System.out.println(words[i]+" is "+words[i].length()+" letters long.");

        }
    }
}

This code uses split() instead of Tokenizer. It just seemed easier to me.

OTHER TIPS

Try this:

import java.util.Scanner;
import java.util.StringTokenizer;

public class WordCount {

 public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.println("PLease enter your phrase");

    String phrase = in.nextLine();
    StringTokenizer st = new StringTokenizer(phrase);
    System.out.println("Your phrase has " + st.countTokens() + " words");
    // Loop thorough to count number of letters in each word.
    while (st.hasMoreTokens()) {
        String tokenName = st.nextToken();
        System.out.println(tokenName + ": has  " + tokenName.length() + " letters");
    }

 }

}

Here is my solution to your problem:

public static void main(String[]args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Please type a phrase.");
    String phrase= in.nextLine();

    // get an array each having a word using split
    String[]words = phrase.split(" ");

    //print count of words?
    System.out.println("Words: "+words.length);

    //loop over the words
    for(int i = 0; i < words.length; i++)
    {
        System.out.println(words[i]+": "+words[i].length()+" letters");
    }  
} 
public class wordCount 
{
    public static void main(String[] args)
    {
    Scanner sc= new Scanner(System.in);
    System.out.println(" Enter a String1");
    String str=sc.nextLine();
    System.out.println(" Entered String : ");
    System.out.println(str);
    //logic
    char ch[]=str.toCharArray();
    str="";
    int count=0;
    for (int i = 0; i < ch.length; i++) 
    {
        if(ch[i]!=' ')
        {
            str=str+ch[i];
            count++;
        }
        else if(ch[i-1]!=' ')
        {
            /*str=str+"-->"+count +" ";*/
            System.out.println(str+"--->"+count);
            count=0;
            str="";
        }
    }
    System.out.println(str+"--->"+count);

sc.close();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top