我在命令上遇到了一些麻烦。
我有一个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.
更多推荐
发布评论