0

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);
    }    
} 
5
  • Other than the typo pointed out in the answer by @Peter777 your code is working without error. I don't think you are actually seeing the error you posted in your question. Perhaps that was generated by previous code. Commented Aug 10, 2017 at 5:55
  • import java.lang.*; - not needed Commented Aug 10, 2017 at 5:56
  • @ScaryWombat Compiles, but won't run, due to a typo: String regex="(//d+?)//1)"; Commented Aug 10, 2017 at 5:56
  • You want us to spend our time to help you solve ýour programming contest entry (strange idea by the way). So you please spend the 1 minute it takes to properly indent/format your code! Commented Aug 10, 2017 at 5:59
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a minimal reproducible example. Use the "edit" link to improve your question - do not add more information via comments. Thanks! Commented Aug 10, 2017 at 5:59

2 Answers 2

2

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).

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

Comments

1

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.

7 Comments

But would not cause the compile problem that the OP is trying to initially fix
@ScaryWombat Fixing this typo gets the code running, not sure if the program is actually working.
make sure your Pattern class is imported from - which it is not because the class is named Pattern
What are you trying to prove here @ScaryWombat? If he takes a look at his imports then he'll find out, won't he?
oops I have pissed off Peter - sorry. No but looking at he imports it looks ok, but he is not using java.util.Regex.Pattern he is using the class that he is writing now which is called Pattern and his class does not have a method called compile hence the compile error
|

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.