【问题标题】:JAVA: Count each word on a String, and count each letter on the wordsJAVA:计算字符串上的每个单词,并计算单词上的每个字母
【发布时间】:2014-03-12 18:10:27
【问题描述】:

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

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

“Nihil Veritas est”
字数:3
Nihil:5 个字母
Veritas:7 个字母
估计: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");


}

【问题讨论】:

  • 你有了一个好的开始。到目前为止,您尝试了什么来打印 Tokenizer 找到的每个单词?从那里开始,计算字母将变得很容易。
  • 改用split()方法不是更方便吗?
  • 到目前为止还没有。我必须遍历令牌,对吗?
  • 好的,我只是在问。我以前从未使用过StringTokenizer。 “迭代令牌”是什么意思?

标签: java string tokenize


【解决方案1】:

试试这个:

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。这对我来说似乎更容易。

【讨论】:

  • 正是我的想法。只有一件小事,words.length() 应该是words.lengthwords[i].length() 是正确的。
  • 非常感谢你们俩。我没有尝试使用split(),因为我不太明白如何使用它。您不仅帮助我完成了练习,而且现在我了解了如何使用split()
  • 太棒了!很高兴能为您提供帮助。
  • 唯一剩下的风险因素是 split(" ") 在单个空格上拆分。所以 (" A "+" B").split(" ") 返回一个包含 4 个元素的数组。为了更加防弹,请使用 (" A "+" B").trim().split("\\s+")。我正在使用字符串加法来解决论坛代码正在重新格式化双空格的事实。
  • @AndrésAvilaWille 没问题!
【解决方案2】:

试试这个:

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

 }

}

【讨论】:

  • 谢谢!我也想知道如何使用标记器来做到这一点。我的疑惑现在都清楚了。
【解决方案3】:

这是我对您问题的解决方案:

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

【讨论】:

    【解决方案4】:
    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2020-11-16
      • 2011-09-05
      • 1970-01-01
      相关资源
      最近更新 更多