JAVA:计算字符串上的每个单词,并计算单词上的每个字母

时间:2014-03-12 18:10:28

标签: java string tokenize

所以,我有一个java赋值,其中我有一个带有短语的String。我需要计算短语的每个单词,然后计算每个单词有多少个字母。我已经能够通过使用Tokenizer将短语分成单词,然后用.countTokens()计算和打印单词的数量。但是,我无法计算每个单词中的字母数。

基本上输出应该是这样的:

  

“Nihil veritas est”
  字:3
  Nihil:5个字母
  Veritas:7个字母
  Est:3个字母

到目前为止,这是我的代码:

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");


}

4 个答案:

答案 0 :(得分:3)

试试这个:

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.");

        }
    }
}

此代码使用split()代替Tokenizer。这对我来说似乎更容易。

答案 1 :(得分:1)

试试这个:

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");
    }

 }

}

答案 2 :(得分:0)

以下是我解决问题的方法:

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");
    }  
} 

答案 3 :(得分:0)

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();
    }
}