syed faisal
syed faisal

Reputation: 11

java regex compile and matcher symbol not found error

i am trying to solve project euler #26 and i am using regex to solve the problem but when compiling i am getting method not found error

import java.lang.*;
import java.math.*;
import java.util.regex.*;

class Pattern {
    public static void main(String args[]) {
        int count = 0;
        String regex = "(//d+?)//1)";
        Pattern p = Pattern.compile(regex); //cannot find symbol compile
        BigDecimal b = new BigDecimal("1");
        for (int i = 1; i <= 10; i++) {
            BigDecimal b1 = new BigDecimal(i);
            String elem = b.divide(b1, 15, RoundingMode.HALF_UP).toString();
            Matcher match = p.matcher(elem); //cannot find symbol matcher
            while (match.find()) {
                int x = match.start() - match.end();
                if (x > count)
                    count = x;
            }
        }
        System.out.println("the highest count is" + count);
    }    
} 

Upvotes: 0

Views: 2461

Answers (2)

nivox
nivox

Reputation: 2130

You have multiple problems:

  1. Your regexp is wrong as @Peter777 pointed out
  2. Your class is named Pattern, same as java.util.regex.Pattern, this causes the compiler to try and use the compile method on your class instead of the java.util.regex one.

To try and solve the problem, fix the regex and rename your class to something else (or import the java.util.regex one with an alias).

Upvotes: 2

Pijotrek
Pijotrek

Reputation: 3021

String regex="(//d+?)//1)"; This regex is for sure incorrect. You have two closing ) and only one (

Also make sure your Pattern class is imported from java.util.Regex package.

Upvotes: 1

Related Questions