当项目本身用空格分隔时,如何分隔由空格组成的文件中的项目?

发布于 2024-12-03 22:13:41 字数 561 浏览 1 评论 0原文

我有一个来自批发商的很长的价格文件,我很难将其读入我的程序,因为每一列都用 x 个空格分隔。像这样;

99995116273       34 mm asasa                                         00472,50100                                                                                               
99998375442       11 lalaaasdsddfgdfgdf                                00503,00206                                                                                             
99998375443       1 1/4 Microkupling                             00867,00206 

如何使用 Java 中的 Scanner 类将每一列分成零件号、描述和价格?

I have a very long price file from my wholesaler that I have som dificulties to read into my program because each column is seperated with x number of white spaces. Like this;

99995116273       34 mm asasa                                         00472,50100                                                                                               
99998375442       11 lalaaasdsddfgdfgdf                                00503,00206                                                                                             
99998375443       1 1/4 Microkupling                             00867,00206 

How can I use the Scanner class in Java to sperate each column into Part no, Description and Price ?

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

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

发布评论

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

评论(5

扛刀软妹 2024-12-10 22:13:41

使用 split 方法。此方法采用正则表达式作为参数,因此这样的东西应该适合您:

String line =....;
String[] colums = line.split("\\s{2,}");

每次找到两个或多个空格时都会创建一个新字符串(空格将被丢弃)。结果将是一个包含您需要的单词的数组。

{2,} 意味着要破坏字符串,它需要有两个或更多空格。

Use the split method. This method takes a regular expression as a parameter, so somthing like this should work for you:

String line =....;
String[] colums = line.split("\\s{2,}");

This will create a new string each time it finds two or more spaces (the spaces will be discarded). The result will be an array containing the words you need.

The {2,} implies that for the string to be broken, it will need to have two or more spaces.

刘备忘录 2024-12-10 22:13:41

早上好,我不是一名java开发人员,但您是否尝试过将值分隔符视为空格,而将其视为“制表符”?我过去处理过制表符分隔的文件,这里可能就是这种情况。

Good morning, I am not a java developer by trade but instead of thinking about the value delimiter as a space, have you tried thinking about it as a "tab"? I have dealt with tab demlimeted files in the past and this could be the case here.

楠木可依 2024-12-10 22:13:41

假设每行有 1 个项目,您可以使用以下内容:

Scanner s = new Scanner(input).useDelimiter("\\n");

因此 s.next 将检索包含项目的字符串,然后单独扫描每一行或简单地拆分它。

Assuming that there's 1 item per line you can use the following:

Scanner s = new Scanner(input).useDelimiter("\\n");

So s.next will retrieve a string containing an item and then scan each line individually or simply split it.

烟凡古楼 2024-12-10 22:13:41

查看粘贴的文本,原始文本似乎使用制表符来对齐列。如果您正在处理的文本包含制表符,并且字段(项目)本身不包含空格,那么您可以再使用一个 tab 字符作为分隔符。

如果制表符已经转换为空格并且结果是上面的输出,那么这将成为一个更加困难的问题,并且只能通过启发式解决。

再次查看文本,格式似乎是

  • 行以零件号开头,这是一系列数字,后跟空格(不是字段的一部分)
  • 行以价格结束,价格在空格之后开始(不是字段 的一部分)字段的一部分)并且是一系列数字后跟一个或多个(命令后跟数字序列)
  • 之间的所有内容都是描述,在修剪两侧的空白之后

如果您可以确认这是格式,那么解决方案不是非常复杂 实施。

Looking at the pasted text it seems the original text is using tab characters to align columns. If the text you are processing has the tabs and the fields (items) themselves do not contain spaces then you can use the one more tab character as the delimiter.

If the tab characters have already been converted to spaces and the result is the above output then this becomes a much more difficult problem and can be solved only heuristically.

Again looking at the text, the fornat seems to be

  • Line begins with a part-number, which is a sequence of digits followed by whitespace (which is not part of the field)
  • Line ends with price, which starts after whitespace (that is not part of the field) and is a sequence of digits followed by one or more (command followed by sequence of digits)
  • Everything in between is description, after trimming whitspaces on both sides

If you can confirm this is the format, then the solution is not very complex to implement.

穿越时光隧道 2024-12-10 22:13:41

与其拆分字符串,为什么不从字符串的开头读取零件号,从末尾读取价格,而中间剩下的就是描述。

Instead of splitting the string why not read the part number from the beginning of the string, the price from the end and what is left in the middle is the description.

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