Ok, I changed the code to find anchor tags instead of removing sections of text. There are two conditions I can think of that would be problematic and if either one happens I don't want to return anything. If the bad conditions don't happen I return a String array with all the anchor tags.
Right now in the code I just return a dummy String array when the bad conditions happen, how should I rewrite the function so that instead of returning a dummy String array it lets me know the task failed? I highlighted the bad conditions that return the dummy array in red:
- String[] source = loadStrings("http://www.processing.org/");
- String singleLine = join(source, "");
- void setup() {
- size(200, 200);
- String[] links = getTags(singleLine, "<a", "</a>");
- println(links);
- }
- void draw() {
- }
- // Always make sure start and end are lowercase
- String[] getTags(String in, String start, String end) {
- ArrayList startIndexes = new ArrayList();
- ArrayList endIndexes = new ArrayList();
- String lowerIn = in.toLowerCase();
- boolean noMatch = false;
- int position = 0;
- // Get all the indexes of start and end keywords
- while (true) {
- int index = lowerIn.indexOf(start, position);
- if (index != -1) {
- startIndexes.add(index);
- position = index+1;
- int indexStop = lowerIn.indexOf(end, position);
- if (indexStop != -1) endIndexes.add(indexStop+end.length());
- else {
- noMatch = true;
- break;
- }
- }
- else break;
- }
- if (startIndexes.size() == 0) {
- println(start+" not found");
- String[] thatsBad = new String[1];
- return thatsBad; // I don't actually want to return this but is causes an error if I don't
- }
- else if (noMatch) {
- println(end+" not found for every case of "+start);
- String[] thatsBad = new String[1];
- return thatsBad; // I don't actually want to return this but is causes an error if I don't
- }
- else {
- String[] subs = new String[startIndexes.size()];
- for (int i = 0; i < subs.length; i++) {
- subs[i] = in.substring((Integer)startIndexes.get(i), (Integer)endIndexes.get(i));
- }
- return subs;
- }
- }