Java Scanner.useDelimiter() 与“”的问题

发布于 2024-09-25 16:09:35 字数 139 浏览 0 评论 0原文

我在 Java Scanner.useDelimiter() 分隔“”时遇到问题。

我正在尝试使用扫描仪读取 CSV 文件。CSV 有 2 列(名称、描述),描述字段有很长的段落(带有 、 和 。)

我想知道这种情况的分隔符是什么。

I have having a problem with Java Scanner.useDelimiter() delimiting "".

I am trying to use the Scanner to read a CSV file.The CSV have 2 column (Name, Description) and the Description field have long paragraphs (with , and .)

I am wondering what delimiter for this case.

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

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

发布评论

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

评论(3

祁梦 2024-10-02 16:09:35

打印之类的怎么样

Scanner s = new Scanner("name1 lorem, some description\n" +
                        "name2 ipsum, some other, description \n" +
                        "name3 dolor, a third. description\n");

while (s.hasNextLine()) {
    String[] cols = s.nextLine().split(",", 2); // limit to two columns.
    System.out.println("col1: " + cols[0] + ", col2:" + cols[1]);
}

col1: name1 lorem, col2: some description
col1: name2 ipsum, col2: some other, description 
col1: name3 dolor, col2: a third. description

或者,如果您坚持一直使用扫描仪,您可以执行类似的操作

Scanner s = new Scanner("name1 lorem, some description\n" +
                        "name2 ipsum, some other, description \n" +
                        "name3 dolor, a third. description\n");

s.useDelimiter(",");

while (s.hasNext())
    System.out.println("col1: " + s.next() + ", col2:" + s.skip(",").nextLine());

(这会产生相同的输出。)

How about something like

Scanner s = new Scanner("name1 lorem, some description\n" +
                        "name2 ipsum, some other, description \n" +
                        "name3 dolor, a third. description\n");

while (s.hasNextLine()) {
    String[] cols = s.nextLine().split(",", 2); // limit to two columns.
    System.out.println("col1: " + cols[0] + ", col2:" + cols[1]);
}

Prints:

col1: name1 lorem, col2: some description
col1: name2 ipsum, col2: some other, description 
col1: name3 dolor, col2: a third. description

Alternatively, if you insist on using Scanner all the way, you could do something like

Scanner s = new Scanner("name1 lorem, some description\n" +
                        "name2 ipsum, some other, description \n" +
                        "name3 dolor, a third. description\n");

s.useDelimiter(",");

while (s.hasNext())
    System.out.println("col1: " + s.next() + ", col2:" + s.skip(",").nextLine());

(Which yields the same output.)

终陌 2024-10-02 16:09:35

只需使用 OpenCSV 之类的库来读取/写入 csv,您无需重新发明轮子:)

Just use a library for reading/writing csv like OpenCSV and you don't have to reinvent the wheel :)

眼睛会笑 2024-10-02 16:09:35

如果您只有两列,以逗号分隔,则 Scanner 的一个简单替代方法是仅使用 String.substring

int i = s.indexOf(',');
String name = s.substring(0,i);
String desc = s.substring(i+1);

If you've only got two columns, separated by a comma, an easy alternative to Scanner is to just use String.substring:

int i = s.indexOf(',');
String name = s.substring(0,i);
String desc = s.substring(i+1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文