2

I'm reading from a CSV file with a structure like this:

1;Miller Hame; 1,2,3,4,5.....; 1232323;

I have to split it to String[], I could work with every line and there with every "Part"

My Code so far:

Stream<String> input = java.nio.file.Files.lines(java.nio.file.Paths.get("data.csv"));

String[] lines = input.skip(1)
        .map(s->s.split(";"))
        .toArray(String[]::new);

Actually I'm getting java.lang.ArrayStoreException.

(Yes, it's for homework, but I don't want the whole solution, only for this very small part of the work.)

2
  • 7
    You have a String[][] not a String[]. Commented Apr 22, 2017 at 16:29
  • 1
    @BoristheSpider yes, replacing .toArray(String[]::new); with .toArray(String[][]::new); should work. Commented Apr 22, 2017 at 16:47

1 Answer 1

6

Something like this

Stream<String> input = Files.lines(Paths.get("src/data.csv"));

String[][] lines = input.skip(1)
        .map(s -> s.split(";"))
        .toArray(String[][]::new);

Advice: You don't have to type full path for a class. You can import it. This is for Files and Paths classes

import java.nio.file.Files;
import java.nio.file.Paths;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.