开发者

java.util.regex.PatternSyntaxException: null (in java.util.regex.Pattern) error

开发者 https://www.devze.com 2023-02-08 17:59 出处:网络
public void convertStrings() { for (int counter = 0; counter < compare.length; counter++) { compare[counter] = compare[counter].replace(\'*\',\'_\');
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; count开发者_StackOverflow中文版er1++) {

            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?


Just change the lines to:

compare[counter] = compare[counter].replaceAll("\\*",".*").replaceAll("\\?", "."); 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号