1

I have data in this format [ [[22,34,56],[12,31,44],[74,18,53]], [[15,16,18],[90,89,74],[44,32,13]], [[13,15,17],[1,4,7],[88,73,10]]...] inside a text file and I want to read it use the numbers in each of the inner list. So far I am able to read each inner list of list with the code below, how can I extend it to get the numbers? This question only handles the case of reading strings into an array list and I have not found anotheer that deals with my case.

File f = new File("route.txt");
Scanner s = new Scanner(new FileInputStream(f));
s.useDelimiter("]],");
while (s.hasNext()) {
    String r = s.next();
    System.out.println(r);
}
Nobi
  • 1,113
  • 4
  • 23
  • 41
  • 2
    Try to parse it as a JSON array. –  Apr 11 '18 at 06:40
  • Using [jackson](https://github.com/FasterXML/jackson-databind): `double[][][] matrix = new ObjectMapper().readValue(new FileInputStream(f), double[][][].class);` – teppic Apr 11 '18 at 07:12

2 Answers2

2

As mentioned if it is a JSON array, you could do that. But then would need to delve in the resulting data structure for processiong. A do-it-yourself solution:

Path path = Paths.get("route.txt");
byte[] bytes = Files.readAllBytes(path);
String content = new String(bytes, StandardCharsets.ISO_8859_1);
content = content.replace(" ", ""); // So spaces at odd places do not need to be handled.
String[] sequences = content.split("[^\\d,]+"); // Delimit by not: digit or comma.
for (String sequence : sequences) {
    if (!sequence.isEmpty()) {
        String[] words = sequence.split(",");
        int[] numbers = Stream.of(words).mapToInt(Integer::parseInt).toArray();
        .. process the sequence(numbers);
    }
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I get an error that processSequence(numbers); is not defned also I assume that for String[] sequences = content.split("[^\\d,]+) the parameter should have a closing string i.e. String[] sequences = content.split("[^\\d,]+") ? – Nobi Apr 11 '18 at 07:17
  • Yes, processNumbers was just an indication where to process one sequence of numbers. Improved answer – Joop Eggen Apr 11 '18 at 07:47
2

Parse it as a JSON array. I recommend JSON-P.

import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonReader;

// ... 

public static void main(String[] args) {
    String data = "[ [[22,34,56],[12,31,44],[74,18,53]], "
            + "[[15,16,18],[90,89,74],[44,32,13]], "
            + "[[13,15,17],[1,4,7],[88,73,10]]]";

    JsonReader jsonReader = Json.createReader(new StringReader(data));
    JsonArray array = jsonReader.readArray();

    for (int i = 0; i < array.size(); i++) {
        JsonArray subArray = array.getJsonArray(i);
        for (int j = 0; j < subArray.size(); j++) {
            JsonArray subSubArray = subArray.getJsonArray(j);
            for (int k = 0; k < subSubArray.size(); k++) {
                System.out.println(String.format("[%d, %d, %d] %d",
                        i, j, k, subSubArray.getInt(k)));
            }
        }
    }
}

Output:

[0, 0, 0] 22
[0, 0, 1] 34
[0, 0, 2] 56
[0, 1, 0] 12
[0, 1, 1] 31
[0, 1, 2] 44
[0, 2, 0] 74
[0, 2, 1] 18
[0, 2, 2] 53
[1, 0, 0] 15
[1, 0, 1] 16
[1, 0, 2] 18
[1, 1, 0] 90
[1, 1, 1] 89
[1, 1, 2] 74
[1, 2, 0] 44
[1, 2, 1] 32
[1, 2, 2] 13
[2, 0, 0] 13
[2, 0, 1] 15
[2, 0, 2] 17
[2, 1, 0] 1
[2, 1, 1] 4
[2, 1, 2] 7
[2, 2, 0] 88
[2, 2, 1] 73
[2, 2, 2] 10
  • The JSON-P dependency seems not to work on my eclipse even after direct download from maven central repo and pasting the ones in your provided link – Nobi Apr 11 '18 at 07:38
  • JsonReader cannot be resolved to a type. Can you perhaps rework your solution using the org.json? – Nobi Apr 11 '18 at 07:46
  • `org.json` is ugly. I recommend to use a modern dependency management tool like Maven, Gradle, or Ivy. –  Apr 11 '18 at 07:54
  • Like I said previously I added the dependency to my pom.xml from the maven central repo – Nobi Apr 11 '18 at 07:55
  • These dependencies work: https://gist.github.com/lutzhorn/592fc1636cc8c6b1316e8a6ea3e3a38e –  Apr 11 '18 at 07:57