Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Unable to use substring() function within an array. Looking for other means.

$
0
0

I am getting an error when I attempt to use substring() as I iterate through an array. I want to extract an integer from a string of text. This works fine outside of an array:

    String trainInfo = "E215\n17:30 - Howth to Bray (-3 mins late)\nArrived Dublin Connolly next stop Tara Street";
    int start      = trainInfo.indexOf("(" ) + 1;  // STEP 1 
    int end        = trainInfo.indexOf(" mins", start);      // STEP 2
    String status  = trainInfo.substring(start, end);    // STEP 3
    int status_no   = int(status);                    // STEP 4
    println(status_no);

Whereas in my case I get an error at the point of the substring():

    void requestData() {   
      for (int i = 0; i < children.length; i++) {     

        XML announcementElement = children[i].getChild("PublicMessage");
        String announcement = announcementElement.getContent(); 

        int start      = announcement.indexOf("(" ) + 1;        // STEP 1 
        int end        = announcement.indexOf(" mins", start);  // STEP 2
        String status  = announcement.substring(start, end);    // STEP 3    ***error thrown at this point***
        int status_no   = int(status);                          // STEP 4

        println(status_no);
      }
    } 

[edit] Though not strictly related to Processing, I found a thread in the following forum that indicates that javascript does not permit substrings within arrays. I guess the same applies to Processing?: https://stackoverflow.com/questions/42429413/javascript-error-array-substring-is-not-a-function

The responder recommends to "slice and then remove the commas" or to use join(''). I have tried to to slice() and join(), based on Processing documentation but it's not working out.

Would anyone know of a way to get around this? Many thanks :)


Viewing all articles
Browse latest Browse all 2896

Trending Articles