0

I am trying to parse through HTML validation errors by adding the errors to an array, and then looping through the array, strip out the first part of the error (e.g. ValidationError line 23 col 40:) and then I want to only keep the text inside the single quotes and save that to a new list.

Here is what I have working, but I know it's not scalable and only works with the String fullText and not with the ArrayList list so that's what I need help with. Thank you!

package htmlvalidator;

import java.util.ArrayList;

public class ErrorCleanup {

public static void main(String[] args) {
    //Saving the raw errors to an array list
    ArrayList<String> list = new ArrayList<String>();

    //Add the text to the first spot
    list.add("ValidationError line 23 col 40:'Bad value ius-cors for attribute name on element meta: Keyword ius-cors is not registered.'");

    //Show what is in the list
    System.out.println("The full error message is: " + list);       

    String fullText = "ValidationError line 23 col 40:'Bad value ius-cors for attribute name on element meta: Keyword ius-cors is not registered.'";

    //Show just the actual message
    System.out.println("The actual error message is: " + fullText.substring(fullText.indexOf("'") + 1));


}

}

2 Answers 2

1

Use a foreach loop:

List<String> list = new ArrayList<String>();
List<String> msgs = new ArrayList<String>();
for (String s : list) {
    msgs.add(s.replaceAll(".*'(.*)'.*", "$1"));
}
list = msgs;

Using regex to extract the string is cleaner and still scalable enough.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use following loop:

    ArrayList<String> errorFullTexts = new ArrayList<>();
    ArrayList<String> errorMessageTexts = new ArrayList<>();

    //Add the text to the first spot, you can add as many as you would like
    errorFullTexts.add("ValidationError line 23 col 40:'Bad value ius-cors for attribute name on element meta: Keyword ius-cors is not registered.'");

    for(String errorFullText : errorFullTexts){
        errorMessageTexts.add(errorFullText.substring(errorFullText.indexOf(":'")+2, errorFullText.lastIndexOf(".'")));
    }

Comments

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.