Java Csv

编程入门 行业动态 更新时间:2024-10-26 06:39:15
Java Csv-data字符串空间拆分错误(Java Csv-data String space split error)

我在命令上遇到了一些麻烦。

我有一个csv类型的文件,如下所示:

Merkmals-Nr。; Interne Teile-Nr。; Bereich; Fertigungsschritt; ...

在阅读File in之后,要读取一行,然后在“;”之后分割行。 通过使用此代码行。

List<String> datenListe = Arrays.asList(data.split(";"));

然后我做一个system.println

打印看起来应该如何: Merkmals-NR。 Interne Teile-Nr。 Bereich Fertigungsschritt ...

打印实际上看起来如何: Merkmals-NR。 上网。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at java.util.Arrays$ArrayList.get(Arrays.java:2866) at CsvEinlesen.main(CsvEinlesen.java:23)

我发现问题是由“Interne Teile-Nr”中的空间引起的。 但我不知道如何解决空间问题。

这是完整的代码:

import java.io.*; import java.util.*; public class CsvEinlesen { public static void main(String[] args) { String fileName = "0-201-08-4473.csv"; File file = new File(fileName); try { Scanner csvInputStream = new Scanner(file); while (csvInputStream.hasNext()) { String data = csvInputStream.next(); List<String> datenListe = Arrays.asList(data.split(";")); for (int i = 0; i < 32; i++) { System.out.println(datenListe.get(i)); } } csvInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("CSV-Datei nicht gefunden"); } } }

I am having some trouble with a command.

I have a file of the type csv which looks like this:

Merkmals-Nr.;Interne Teile-Nr.;Bereich;Fertigungsschritt;...

After reading the File in is want to read one Line and then split the line after ";" by using this codeline.

List<String> datenListe = Arrays.asList(data.split(";"));

Then I do a system.println

How the print should look: Merkmals-Nr. Interne Teile-Nr. Bereich Fertigungsschritt ...

How the print actually looks: Merkmals-Nr. Interne

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at java.util.Arrays$ArrayList.get(Arrays.java:2866) at CsvEinlesen.main(CsvEinlesen.java:23)

I figured out that the problem is caused by the space in "Interne Teile-Nr." but I don´t know how to solve the problem with the spaces.

This is thecomplete code:

import java.io.*; import java.util.*; public class CsvEinlesen { public static void main(String[] args) { String fileName = "0-201-08-4473.csv"; File file = new File(fileName); try { Scanner csvInputStream = new Scanner(file); while (csvInputStream.hasNext()) { String data = csvInputStream.next(); List<String> datenListe = Arrays.asList(data.split(";")); for (int i = 0; i < 32; i++) { System.out.println(datenListe.get(i)); } } csvInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("CSV-Datei nicht gefunden"); } } }

最满意答案

如果仍然像普通数组一样遍历它,是否真的有必要将数组转换为List<String> ? 你为什么把32作为限制? 这是不安全的 - 正是因为你最终会得到像ArrayIndexOutOfBoundsException这样的错误。

对于这个例子,我的建议就是像这样使用数组:

//the rest of your code... while (csvInputStream.hasNext()) { String data = csvInputStream.next(); String[] wordsInLine = data.split(";"); for (int i = 0; i < wordsInLine.length; i++) { System.out.println(wordsInLine[i]); } } //the rest of your code ...

试试看看错误是否消失。

Is it really necessary to convert the array to a List<String> if you still iterate through it like a normal array? Also why did you put 32 as a limit? This is not safe - exactly because you will end up getting errors like ArrayIndexOutOfBoundsException.

My suggestion, for this example, is to just work with the array like this:

//the rest of your code... while (csvInputStream.hasNext()) { String data = csvInputStream.next(); String[] wordsInLine = data.split(";"); for (int i = 0; i < wordsInLine.length; i++) { System.out.println(wordsInLine[i]); } } //the rest of your code ...

Give that a try and see if the error goes away.

更多推荐

本文发布于:2023-08-08 00:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   Csv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!