-4

I have the following string :

"[[0, 0, 0], [1, 1, 1], [2, 2, 2]]"

What is the easiest way in Java to convert this to a pure multidimensional float array? Somthing like this for example:

String stringArray = "[[0, 0, 0], [1, 1, 1], [2, 2, 2]]";
float[][] floatArray = stringArray.parseSomeHow() //here I don't know the best way to convert

Sure I could write an algorithm that would read for example each char or so. But maybe there is a simpler and faster way that java already provides.

  • 1
    Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  May 18 '17 at 14:19
  • 1
    Please read [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) before attempting to ask more questions. –  May 18 '17 at 14:20
  • why not pass the "stringArray" in an actual string array? – XtremeBaumer May 18 '17 at 14:20
  • Please read [Why is asking a question on “best practice” a bad thing?](https://meta.stackexchange.com/questions/142353/why-is-asking-a-question-on-best-practice-a-bad-thing/243450) before attempting to ask more questions that are opinion based that invite argumentative discussion because they do not have a single agreed upon answer. –  May 18 '17 at 14:20
  • Sry for the wrong way to ask. What I wanted to know was if there is somthing similar like Integer.parse(someStringWithNumbers) for this situation. But since multiple algorithms showed up, I know that there is no such thing. Thanks for the replies. – Wickthor Battlecow May 19 '17 at 08:41

2 Answers2

2

A "pseudocode" from the top of my mind:

1- Get rid of the first and last characters (e.g: remove the first "[" and the last "]").

2- Use regex to find the text between brackets.

3- Loop over the matches of step 2 and split them by the "," character.

4- Loop over the splitted String and trim the value before casting it into a float and then put that value in the array in the correct position.


A code sample

public static void main(String[] args) {
    String stringArray = "[[0, 0, 0], [1, 1, 1], [2, 2, 2]]";

    //1. Get rid of the first and last characters (e.g: remove the first "[" and the last "]").

    stringArray = stringArray.substring(1, stringArray.length() - 1);

    //2. Use regex to find the text between brackets.
    Pattern pattern = Pattern.compile("\\[(.*?)\\]");
    Matcher matcher = pattern.matcher(stringArray);

    //3. Loop over the matches of step 2 and split them by the "," character.
    //4.  Loop over the splitted String and trim the value before casting it into a float and then put that value in the array in the correct position.

    float[][] floatArray = new float[3][3];
    int i = 0;
    int j = 0;
    while (matcher.find()){
        String group = matcher.group(1);
        String[] splitGroup = group.split(",");
        for (String s : splitGroup){
            floatArray[i][j] = Float.valueOf(s.trim());
            j++;
        }
        j = 0;
        i++;
    }
    System.out.println(Arrays.deepToString(floatArray));
   //This prints out [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]
}
Community
  • 1
  • 1
Averroes
  • 4,168
  • 6
  • 50
  • 63
1

Here's one way to implement it:

public static float[][] toFloatArray(String s){
    String [] array = s.replaceAll("[\\[ ]+", "").split("],");

    float [][] floatArray = new float[array.length][];

    for(int i = 0; i < array.length; i++){
        String [] row = array[i].split("\\D+");
        floatArray[i] = new float[row.length];
        for(int j = 0; j < row.length; j++){
            floatArray[i][j] = Float.valueOf(row[j]);
        }           
    }

    return floatArray;
}

Using Java 8 Streams, here's another way to do this:

public static Float[][] toFloatArray2(String s) {
    return Pattern.compile("[\\[\\]]+[,]?")
            .splitAsStream(s)
            .filter(x -> !x.trim().isEmpty())
            .map(row -> Pattern.compile("\\D+")
                        .splitAsStream(row)
                        .map(r -> Float.valueOf(r.trim()))
                        .toArray(Float[]::new)
            )
            .toArray(Float[][]::new);
}
rrufai
  • 1,475
  • 14
  • 26