0

Sorry for my noob question.. I have String with multpile lines

String is like this:

1   kave    Topli napitci   7
2   sokovi  Sokovi  12
3   pive    Pive    14
4   žestice     Domaæa Žestoka P    16
7   ostalo  Hrana   50
8   vino    Vino    33
9   6   Strana Žestoka P    34

What i want to get 2d array for every word.

I do something like this:

String[] lines =string.split(System.lineSeparator());

String[] parts =  string[0].split("\t");
String[] parts1 = string[1].split("\t");
String[] parts2 = string[2].split("\t"); ....

Please help how to do this in 2d array.

4
  • Use a for loop to navigate the lines array. Commented Oct 5, 2017 at 19:18
  • Can you show some code output please mister? Commented Oct 5, 2017 at 19:21
  • @NikolaBozic is this string in a file? Or is it assigned to a variable in your program? Commented Oct 5, 2017 at 19:30
  • Its a variable in program , but parsed from file. Commented Oct 5, 2017 at 19:36

2 Answers 2

1
List<String[]> lines = Arrays.stream(string.split(System.lineSeparator())).map(line -> line.split("\t")).collect(Collectors.toList());
lines.toArray(new String[lines.size()][]);
Sign up to request clarification or add additional context in comments.

Comments

0

I think it would be better to assign the values directly to the array (but use ArrayList) while parsing from the file with something like this:

ArrayList<String[]> array = new ArrayList<String[]>;

while (file.hasNextLine()) {
   String str = file.nextLine();
   String[] strArr = str.split("\t");
   array.add(strArr);
}

This should give you an ArrayList filled with String Arrays that are split at the "\t" delimeter.

1 Comment

Yeah but this String is not complete file, the complete file has many more lines, this string is cut from wholw file.

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.