【问题标题】:Char count of each token in Tokenized String, Java标记化字符串中每个标记的字符数,Java
【发布时间】:2020-09-14 02:13:15
【问题描述】:

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

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

}

}

控制台示例:

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

你好,这是一个测试

谢谢。

您输入了:

你好,这是一个测试

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

【问题讨论】:

  • 编译器或你的IDE告诉你你的代码是否编译。如果没有,他们会给你错误信息?!
  • 但如果没有真正的minimal reproducible example,我们真的无法判断...
  • @GhostCat 抱歉,我不清楚。我没有收到错误消息,但没有得到我想要打印到控制台的信息。我会用一个例子来更新我的问题。

标签: java arraylist char tokenize stringtokenizer


【解决方案1】:

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

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

}

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

最后,为了解决这个问题,您可以简单地遍历您在倒数第二个 while 循环中添加的列表,然后简单地循环遍历该列表以进行最终循环!

例如:

  int totalLength = 0;

  for (String each : tokenizedInput) {

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

  }

【讨论】:

  • 抱歉没有包括进口,我没有考虑到这一点。它们在我的代码中。你的解释很清楚。对此,我真的非常感激。我觉得我是如此接近,但我永远不会到达那里。再次感谢您的帮助。
  • 没问题,下次一定要加进去。你几乎就在那里,发生在我们所有人身上。如有任何其他问题,请随时提出!
  • 我希望将来能像您一样对像我这样的人有所帮助。再次感谢!
  • 很高兴听到这个消息。随时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
相关资源
最近更新 更多