标记化字符串中每个标记的字符数,Java

标记化字符串中每个标记的字符数,Java,java,arraylist,char,tokenize,stringtokenizer,Java,Arraylist,Char,Tokenize,Stringtokenizer,我试图弄清楚是否可以计算每个令牌的字符数并显示这些信息,例如: day是标记化的,我的输出将是:“day有3个字符。”并继续为每个标记这样做 我最后一次打印每个标记中字符的循环从不打印: public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<String> tokenizedInput = new ArrayList<>();

我试图弄清楚是否可以计算每个令牌的字符数并显示这些信息,例如:

day是标记化的,我的输出将是:“day有3个字符。”并继续为每个标记这样做

我最后一次打印每个标记中字符的循环从不打印:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    ArrayList<String> tokenizedInput = new ArrayList<>();
    String sentenceRetrieved;

    // getting the sentence from the user
    System.out.println("Please type a sentence containing at least 4 words, with a maximum of 8 words: ");
    sentenceRetrieved = sc.nextLine();
    StringTokenizer strTokenizer = new StringTokenizer(sentenceRetrieved);

    // checking to ensure the string has 4-8 words
    while (strTokenizer.hasMoreTokens()) {
        if (strTokenizer.countTokens() > 8) {
            System.out.println("Please re-enter a sentence with at least 4 words, and a maximum of 8");
            break;

        } else {
            while (strTokenizer.hasMoreTokens()) {
                tokenizedInput.add(strTokenizer.nextToken());
            }

            System.out.println("Thank you.");
            break;
        }
    }

    // printing out the sentence
    System.out.println("You entered: ");
    System.out.println(sentenceRetrieved);

    // print out each word given
    System.out.println("Each word in your sentence is: " + tokenizedInput);

    // count the characters in each word
    // doesn't seem to run

    int totalLength = 0;
    while (strTokenizer.hasMoreTokens()) {
        String token;
        token = sentenceRetrieved;
        token = strTokenizer.nextToken();
        totalLength += token.length();
        System.out.println("Word: " + token + " Length:" + token.length());
    }

}

}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
ArrayList tokenizedInput=新的ArrayList();
检索字符串语句;
//从用户处获取句子
System.out.println(“请键入一个包含至少4个单词的句子,最多8个单词:”);
语句检索=sc.nextLine();
StringTokenizer strTokenizer=新的StringTokenizer(语句检索);
//检查以确保字符串包含4-8个单词
while(strTokenizer.hasMoreTokens()){
if(strTokenizer.countTokens()>8){
System.out.println(“请重新输入至少4个单词的句子,最多8个”);
打破
}否则{
while(strTokenizer.hasMoreTokens()){
添加(strTokenizer.nextToken());
}
System.out.println(“谢谢”);
打破
}
}
//把句子打印出来
System.out.println(“您输入:”);
System.out.println(语句检索);
//把每个单词都打印出来
System.out.println(“句子中的每个单词都是:“+tokenizedInput”);
//计算每个单词中的字符数
//好像没有跑
int-totalength=0;
while(strTokenizer.hasMoreTokens()){
字符串标记;
token=检索到的语句;
token=strTokenizer.nextToken();
totalLength+=标记.长度();
System.out.println(“单词:“+token+”长度:“+token.Length()”);
}
}
}
控制台示例:

请键入至少包含4个单词的句子,最多8个单词:

你好,这是一个测试

多谢各位

您输入:

你好,这是一个测试


句子中的每个单词都是:[你好,这里,这,是,a,测试]

首先,我添加了必要的导入,并围绕这个主要方法构建了一个类。这应该可以编译

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

public class SOQ_20200913_1
{

   public static void main(String[] args) {
   
      Scanner sc = new Scanner(System.in);
   
      ArrayList<String> tokenizedInput = new ArrayList<>();
      String sentenceRetrieved;
   
    // getting the sentence from the user
      System.out.println("Please type a sentence containing at least 4 words, with a maximum of 8 words: ");
      sentenceRetrieved = sc.nextLine();
      StringTokenizer strTokenizer = new StringTokenizer(sentenceRetrieved);
   
    // checking to ensure the string has 4-8 words
      while (strTokenizer.hasMoreTokens()) {
         if (strTokenizer.countTokens() > 8) {
            System.out.println("Please re-enter a sentence with at least 4 words, and a maximum of 8");
            break;
         
         } else {
            while (strTokenizer.hasMoreTokens()) {
               tokenizedInput.add(strTokenizer.nextToken());
            }
         
            System.out.println("Thank you.");
            break;
         }
      }
   
    // printing out the sentence
      System.out.println("You entered: ");
      System.out.println(sentenceRetrieved);
   
    // print out each word given
      System.out.println("Each word in your sentence is: " + tokenizedInput);
   
    // count the characters in each word
    // doesn't seem to run
   
      int totalLength = 0;
      while (strTokenizer.hasMoreTokens()) {
         String token;
         token = sentenceRetrieved;
         token = strTokenizer.nextToken();
         totalLength += token.length();
         System.out.println("Word: " + token + " Length:" + token.length());
      }
   
   }

}

编译器或IDE会告诉您代码是否可以编译。如果没有,他们会给你错误信息?!但是如果没有一个真实的答案,我们就不能真正说出…@幽灵猫对不起,我不清楚。我没有得到一个错误,但是我没有得到我想要打印到控制台的信息。我将用一个例子来更新我的问题。很抱歉没有包括导入,我没有想到这一点。它们在我的代码中。你的解释很清楚。我真的很感激。我觉得我离这里很近,但我永远都不会到达那里。再次感谢您的帮助。没问题,下次一定要包括它们。你就快到了,我们所有人都会这样。请随时提出您可能有的任何其他问题!我希望将来能像你一样帮助像我这样的人。再次感谢!很高兴听到这个消息。随时
  int totalLength = 0;

  for (String each : tokenizedInput) {

     totalLength += each.length();
     System.out.println("Word: " + each + " Length:" + each.length());

  }