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);
}
2
  • 2
    Try to parse it as a JSON array. Commented Apr 11, 2018 at 6:40
  • Using jackson: double[][][] matrix = new ObjectMapper().readValue(new FileInputStream(f), double[][][].class); Commented Apr 11, 2018 at 7:12

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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,]+") ?
Yes, processNumbers was just an indication where to process one sequence of numbers. Improved answer
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

5 Comments

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
JsonReader cannot be resolved to a type. Can you perhaps rework your solution using the org.json?
org.json is ugly. I recommend to use a modern dependency management tool like Maven, Gradle, or Ivy.
Like I said previously I added the dependency to my pom.xml from the maven central repo

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.