fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9. public static void main(String[] args) {
  10. String input = "[[\"X\",\"X\",\"X\",\"X\"],[\"X\",\"O\",\"O\",\"X\"],[\"X\",\"X\",\"O\",\"X\"],[\"X\",\"O\",\"X\",\"X\"]]";
  11.  
  12. // Remove leading and trailing brackets from the input string
  13. input = input.substring(1, input.length() - 1);
  14.  
  15. // Split the input string into rows based on the comma outside quotes
  16. String[] rows = input.split("\\],");
  17.  
  18.  
  19.  
  20. // Create a 2D array to store the elements
  21. String[][] array2D = new String[rows.length][];
  22.  
  23. for (int i = 0; i < rows.length; i++) {
  24. // Add back the closing bracket for the last row
  25. if (i == rows.length - 1) {
  26. rows[i] += "]";
  27. }
  28.  
  29. // Remove leading and trailing brackets from each row
  30. rows[i] = rows[i].substring(1, rows[i].length() - 1);
  31. System.out.println(rows[i]);
  32.  
  33. // Split each row into elements based on the comma outside quotes
  34. array2D[i] = rows[i].split(",\"");
  35. }
  36.  
  37. // Print the 2D array
  38. for (String[] row : array2D) {
  39. for (String element : row) {
  40. System.out.print(element + " ");
  41. }
  42. System.out.println();
  43. }
  44. }
  45. }
  46.  
Success #stdin #stdout 0.14s 57592KB
stdin
Standard input is empty
stdout
"X","X","X","X
"X","O","O","X
"X","X","O","X
"X","O","X","X"]
"X" X" X" X 
"X" O" O" X 
"X" X" O" X 
"X" O" X" X"]