让 stringTokenizer 将一行文本拆分为预定义变量的最佳方法是什么
我不确定标题是否很清楚,但基本上我要做的就是从文件中读取一行文本并将其分成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好的方法是根本不使用 StringTokenizer,而是使用 String 的 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:
tokens
will now have 6 - 8 Strings. Usetokens.length()
to find out how many, then create your object from the array.正则表达式就是方法。 您可以使用 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)
带有捕获组的正则表达式对您有用吗? 您当然可以将表达式的某些部分设为可选。
一行或三行数据示例可能会有所帮助。
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.
这是 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.