让 stringTokenizer 将一行文本拆分为预定义变量的最佳方法是什么

发布于 2024-07-07 12:25:12 字数 534 浏览 5 评论 0原文

我不确定标题是否很清楚,但基本上我要做的就是从文件中读取一行文本并将其分成 8 个不同的字符串变量。 每行将有相同顺序的 8 个块(标题、作者、价格等)。 因此,对于每一行文本,我希望最终得到 8 个字符串。

第一个问题是该行的最后两个字段可能存在也可能不存在,所以我需要对 stringTokenizer.hasMoreTokens 做一些事情,否则当字段 7 和 8 不存在时它会混乱地死掉。

理想情况下,我希望在 for 循环的 while 中完成此操作,但我不确定如何告诉该循环字段的顺序,以便它可以正确填充所有 8 个(或 6 个)字符串。 请告诉我有一个比使用 8 个嵌套 if 语句更好的方法!

编辑: String.split 解决方案似乎绝对是其中的一部分,所以我将使用它而不是 stringTokenizer。 但是,我仍然不确定将各个字符串输入构造函数的最佳方法是什么。 最好的方法是让类需要一个数组,然后在构造函数中执行类似的操作:

line[1] = isbn;
line[2] = title;

I'm not sure if the title is very clear, but basically what I have to do is read a line of text from a file and split it up into 8 different string variables. Each line will have the same 8 chunks in the same order (title, author, price, etc). So for each line of text, I want to end up with 8 strings.

The first problem is that the last two fields in the line may or may not be present, so I need to do something with stringTokenizer.hasMoreTokens, otherwise it will die messily when fields 7 and 8 are not present.

I would ideally like to do it in one while of for loop, but I'm not sure how to tell that loop what the order of the fields is going to be so it can fill all 8 (or 6) strings correctly. Please tell me there's a better way that using 8 nested if statements!

EDIT: The String.split solution seems definitely part of it, so I will use that instead of stringTokenizer. However, I'm still not sure what the best way of feeding the individual strings into the constructor. Would the best way be to have the class expecting an array, and then just do something like this in the constructor:

line[1] = isbn;
line[2] = title;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少钕鈤記 2024-07-14 12:25:12

最好的方法是根本不使用 StringTokenizer,而是使用 String 的 split 方法。 它返回一个字符串数组,您可以从中获取长度。

对于文件中的每一行,您可以执行以下操作:

String[] tokens = line.split("#");

tokens 现在将有 6 - 8 个字符串。 使用 tokens.length() 找出有多少,然后从数组创建对象。

The best way is to not use a StringTokenizer at all, but use String's split method. It returns an array of Strings, and you can get the length from that.

For each line in your file you can do the following:

String[] tokens = line.split("#");

tokens will now have 6 - 8 Strings. Use tokens.length() to find out how many, then create your object from the array.

滥情哥ㄟ 2024-07-14 12:25:12

正则表达式就是方法。 您可以使用 split 方法将传入的 String 转换为 String 数组

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

Regular expression is the way. You can convert your incoming String into an array of String using the split method

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

一口甜 2024-07-14 12:25:12

带有捕获组的正则表达式对您有用吗? 您当然可以将表达式的某些部分设为可选。

一行或三行数据示例可能会有所帮助。

Would a regular expression with capture groups work for you? You can certainly make parts of the expression optional.

An example line of data or three might be helpful.

云醉月微眠 2024-07-14 12:25:12

这是 CSV 或类似文件吗? 如果是这样,有一些库可以帮助您,例如 Apache Commons CSV (链接到替代方案也在他们的页面上)。 它将为您提供文件中每一行的 String[]。 只需检查数组大小即可了解存在哪些可选字段。

Is this a CSV or similar file by any chance? If so, there are libraries to help you, for example Apache Commons CSV (link to alternatives on their page too). It will get you a String[] for each line in the file. Just check the array size to know what optional fields are present.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文