0
public void convertStrings() {
    for (int counter = 0; counter < compare.length; counter++) {
            compare[counter] = compare[counter].replace('*','_'); 
            compare[counter] = compare[counter].replaceAll("_",".*"); 
            compare[counter] = compare[counter].replace('?', '.'); 
    }
    // System.out.printf("%s", Arrays.toString(compare));
} 
public void compareStrings() {
    for (int counter = 0; counter < data.length; counter++) {
        for (int counter1 = 0; counter1 < compare.length; counter1++) {

            if (data[counter].matches(compare[counter1]) == true) {
                System.out.printf("%s ", data[counter]); 
            }

        }
        System.out.println(); 
    }
}

}

what i'm trying to do is replace any * in my input to a .* so that when i compare the string to anything before, it'll ignore previous characters. Also, i'm converting a "?" into a placeholder value ".". However, when i run the compiled code, I get this error because the string converts the special characters into regular letters. How do i make the compiler register these special characters to perform the function?

2 Answers 2

1

Just change the lines to:

compare[counter] = compare[counter].replaceAll("\\*",".*").replaceAll("\\?", "."); 
Sign up to request clarification or add additional context in comments.

Comments

0

I am assuming that the error is being thrown from the line

if (data[counter].matches(compare[counter1]) == true)

If so, the most likely explanation is that compare[counter1] is actually null, that is it does not contain a value.

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.