stringtokenizer 和未知变量类型
我目前正在开发一个项目,在该项目中我流式传输文本文档并将其标记化。唯一的问题是文本文档中的类型未知,在将其设置为程序中的变量之前有什么方法可以检查它是什么类型吗?
I’m currently working on a project in which I stream in a text document and tokenize it. The only problem is the types in the text document is unknown, is there any way to check what variable type it is before I set it to a variable in the program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
否,因为类型信息不包含在文本流中。例如,假设分词器遇到以下标记:
这是什么类型?它可以是字符串 (
"47"
)、字节、int、long、float 或 double。所有这些类型都可以生成该令牌,因此在打印之前无法判断该类型是什么。当您解析文件时,您应该已经知道需要什么类型,如果不匹配则给出错误。
StringTokenizer 类仅返回字符串。如果您需要字符串,则可以将它们保存到变量中。如果您需要其他类型,则必须解析它。例如,如果您读取字符串
"47"
,那么您应该通过 Integer.parseInt。这将返回一个 int (例如,47
),或者如果不匹配则抛出 NumberFormatException。您可能想要捕获 NumberFormatException 并向用户提供错误,因为文本文件与您期望的不匹配。No, because the type information is not contained in the text stream. For example, imagine the tokeniser encounters the following token:
What type does this have? It could be a String (
"47"
), a byte, an int, a long, a float or a double. All of those types could have produced that token, so there is no way to tell what the type was before it was printed.When you parse a file, you should already know what types to expect, and give an error if it does not match.
The StringTokenizer class only gives you back Strings. If you are expecting Strings, you can just save them to a variable. If you are expecting another type, you must parse it. For example, if you read the string
"47"
, then you should run it through Integer.parseInt. This will either return an int (e.g.,47
), or throw a NumberFormatException if it didn't match. You might want to catch NumberFormatException and give the user an error, since the text file didn't match what you were expecting.