令牌化字符串中每个令牌的字符计数,Java

huangapple go评论70阅读模式
英文:

Char count of each token in Tokenized String, Java

问题

我试图弄清楚是否可以计算每个标记的字符数并显示该信息,例如:

"day" 被标记化,我的输出将是:"Day has 3 characters.",并继续为每个标记执行此操作。

我的最后一个循环来打印出每个标记中的字符数从未打印出:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

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

    // 从用户获取句子
    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);

    // 检查以确保字符串有4-8个单词
    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;
        }
    }

    // 打印句子
    System.out.println("You entered: ");
    System.out.println(sentenceRetrieved);

    // 打印出每个给定的单词
    System.out.println("Each word in your sentence is: " + tokenizedInput);

    // 计算每个单词中的字符数
    // 似乎没有运行

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

}

}

示例控制台输出:

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

Hello there this is a test

谢谢。

你输入的是:

Hello there this is a test

你句子中的每个单词是:[Hello, there, this, is, a, test]

英文:

I'm trying to figure out if I can count the characters of each token and display that information such as:

day is tokenized and my output would be: "Day has 3 characters." and continue to do that for each token.

My last loop to print out the # of characters in each token never prints:

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

Example of Console:

Please type a sentence containing at least 4 words, with a maximum of 8 words:

Hello there this is a test

Thank you.

You entered:

Hello there this is a test

Each word in your sentence is: [Hello, there, this, is, a, test]

答案1

得分: 0

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

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;

      // 从用户获取句子
      System.out.println("请输入一个包含至少4个单词、最多8个单词的句子:");
      sentenceRetrieved = sc.nextLine();
      StringTokenizer strTokenizer = new StringTokenizer(sentenceRetrieved);

      // 检查确保字符串有4-8个单词
      while (strTokenizer.hasMoreTokens()) {
         if (strTokenizer.countTokens() > 8) {
            System.out.println("请重新输入一个包含至少4个单词、最多8个单词的句子:");
            break;

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

            System.out.println("谢谢您。");
            break;
         }
      }

      // 打印句子
      System.out.println("您输入的是:");
      System.out.println(sentenceRetrieved);

      // 打印出每个给定的单词
      System.out.println("您的句子中的每个单词是:" + tokenizedInput);

      // 计算每个单词中的字符数
      // 似乎不会运行

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

   }

}

接下来让我们看看这个工作示例似乎一切都可以正常工作直到最后一个循环之前的那个循环计算字符长度的循环)。但是如果您注意到在最后一个循环之前前一个循环将继续循环直到它没有*更多的标记*可获取为止因此在它收集了所有标记并且没有更多标记可收集之后您尝试创建最后一个循环要求它获取更多标记在它没有达到最后一个循环之前它不会用尽标记

最后为了解决这个问题您只需遍历在倒数第二个循环中添加的列表并在最后一个循环中简单地遍历该列表

例如

```java
int totalLength = 0;

for (String each : tokenizedInput) {

   totalLength += each.length();
   System.out.println("单词:" + each + " 长度:" + each.length());

}
英文:

First off, I have added the necessary imports and built a class around this main method. This should compile.

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

Next, let's look at this working example. It seems like everything up until your final while loop (the one that counts character length) works just fine. But if you notice, the while loop before the final one will continue looping until it has no more tokens to fetch. So, after it has finished gathering all of the tokens and has no more tokens to gather, you try and create the final while loop, asking it to gather more tokens. It would not have reached the while loop until it ran out of tokens to gather!

Finally, in order to solve this, you can simply go through the list that you added to in the second to last while loop, and simply cycle through that for your final loop!

For example:

  int totalLength = 0;
for (String each : tokenizedInput) {
totalLength += each.length();
System.out.println(&quot;Word: &quot; + each + &quot; Length:&quot; + each.length());
}

huangapple
  • 本文由 发表于 2020年9月14日 10:13:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63877270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定