0
How do I resolve my 'java: illegal start of type' error?
The below code gives me an error saying ''java: illegal start of type". Pls, help import java.util.regex.*; public class Regex { String pattern = "[a-z]+"; String textToCheck = "Hi there, I'm not Carl"; String compiledPattern = Pattern.compile(pattern); String m = compiledPattern.matcher(textToCheck); while (m.find()){ System.out.println(textToCheck.substring(m.start(), m.end())); } } }
3 odpowiedzi
+ 2
Where is main method?
Atleast You should write all definations and Calculations in a method, to compile...
+ 1
Thx @JayakridhnaIN. Here's the code all fixed up, and I made the name of the .java class the same as the name of the class in its declaration. Here's the working code:
import java.util.regex.*;
public class Regexp {
public static void main(String[] args) {
String pattern = "[a-z]+";
String textToCheck = "Hi there, I'm not Carl";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher m = compiledPattern.matcher(textToCheck);
while (m.find()) {
System.out.println(textToCheck.substring(m.start(), m.end()));
}
}
}
0
for whole words
String pattern = "[\\w']+";