Skip to content
Advertisement

How to split string of mulitdimensional array

i have string here String str1 = "{{1,2,3},{4,5,6},{7,8,9}}"

and the result i expect is like this

JavaScript

what method i use in java language? Thanks.


i tried with split method then put each array into an arraylist variable “data”. result :

JavaScript

and try to delete the data array that is empty and which only has a string value”,” result :

JavaScript

and this is my code :

JavaScript

Advertisement

Answer

You get the StringIndexOutOfBoundsException because you have some empty strings in your arrOfStr1 array. The n.charAt(0) expression is causing the exception because there is no characters in some of the strings. Use the debugger to see whats really in the data array before you try to remove the single commas.

Anyways, here is the fix, modify one line in your code:

JavaScript

And by the way you could also use Streams. The whole code could also look like that:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement