-2

I have the following service for to get values inside a string document, this service is called inside a for, getting the data for every flight and then generate a PDF.

I'm getting the Index 2 out of bounds for length 1 when try to call the service, this is the code:

private Map<String, Object> readFileLsd(String content) {
    Map<String, Object> mapResult = new LinkedHashMap<>();
    try {
        Reader inputString = new StringReader(content);
        BufferedReader br = new BufferedReader(inputString);
        String line;
        String TotalBaggagesCargo = "";
        String PASSENGER = "";
        String TOTAL_TRAFFIC = "";
        String validCharacters = "[\\x00-\\x1F]|[\\x21-\\x2c]|[\\x3B-\\x40]|[\\x5B-\\x60]|[\\x7B-\\xFF]";
        while ((line = br.readLine()) != null) {
            line = line.replaceAll(validCharacters, "").trim();
            if (line.startsWith("LOAD IN COMPARTMENTS")) {
                TotalBaggagesCargo = line;
            }
            if (line.startsWith("PASSENGER/CABIN BAG")) {
                PASSENGER = line;
            }
            if (line.startsWith("TOTAL TRAFFIC LOAD")) {
                TOTAL_TRAFFIC = line;
            }
        }
        mapResult.put("TotalBaggagesCargo", TotalBaggagesCargo.trim().replaceAll("\\s+", " ").split(" ")[3]);
        mapResult.put("PASSENGER", PASSENGER.trim().replaceAll("\\s+", " ").split(" ")[2]);
        mapResult.put("TOTAL_TRAFFIC", TOTAL_TRAFFIC.trim().replaceAll("\\s+", " ").split(" ")[3]);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mapResult;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • the result of one of your `split`s does not return what you expect it to return. – f1sh Jun 21 '22 at 19:37
  • Which would be the solution? – Andres Hernandez Jun 21 '22 at 22:39
  • Not related to your problem, but you can replace `.replaceAll("\\s+", " ").split(" ")` with `split("\\s+")`. As to solving it, inspect the values you are splitting to see if they match your expectation, and if the array returned by split matches your expectation. Also keep in mind, the first index is `0`, the second index is `1`! – Mark Rotteveel Jun 22 '22 at 10:05

1 Answers1

2

Underlying Problem

The line which starts with PASSENGER/CABIN BAG apparently has only one whitespace character and when you split it by a space it results in a String array with only one entry.

Possible solution

If the amount of passengers is sometimes not present in the input String, then you could make the put of key conditional.

String[] passengers = PASSENGER.trim().replaceAll("\\s+", " ").split(" ");
if (passenger.length > 2) mapResult.put("PASSENGER", passengers[2]);

This might bring different problems later in your program. So before working around it, you must try to understand, why it is absent. If it is reasonable that it is missing, then the workaround is acceptable, maybe you will need an else-case like that

else mapResult.put("PASSENGER", "");

when the key has to be present later on.

Valerij Dobler
  • 1,848
  • 15
  • 25