gpt4 book ai didi

java - 如何在不使用 java.util 分词器的情况下在 Java 中进行分词?

转载 作者:行者123 更新时间:2023-11-30 07:36:10 25 4
gpt4 key购买 nike

Consider the following as tokens:

  1. +, -, ), (
  2. alpha charactors and underscore
  3. integer

Implement 1.getToken() - returns a string corresponding to the next token 2.getTokPos() - returns the position of the current token in the input string

Example input: (a+b)-21)
Output: (| a| +| b| )| -| 21| )|

Note: Cannot use the java string tokenizer class

正在进行中 - 成功标记化 +,-,),(。需要弄清楚字符和数字:

输出:+|-|+|-|(|(|)|)|)|(| |

最佳答案

java.util tokenizer 是一个已弃用的类。

自 Java 1.4 以来,使用“String.split()”在 Java 中标记字符串要容易得多:

String[] tokens = "(a+b)-21)".split("[+-)(]");

如果是作业,你可能需要重新实现一个“split”方法:

  • 逐字符读取字符串
  • 如果字符不是特殊字符,将其添加到缓冲区
  • 遇到特殊字符时,将缓冲区内容添加到列表中并清空缓冲区

因为它(可能)是作业,所以我让你实现它。

关于java - 如何在不使用 java.util 分词器的情况下在 Java 中进行分词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3859497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com