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())); } } }

30th Jan 2022, 2:16 PM
Ayomisesebere
Ayomisesebere - avatar
3 odpowiedzi
+ 2
Where is main method? Atleast You should write all definations and Calculations in a method, to compile...
30th Jan 2022, 2:43 PM
Jayakrishna 🇮🇳
+ 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())); } } }
30th Jan 2022, 7:40 PM
Ayomisesebere
Ayomisesebere - avatar
0
for whole words String pattern = "[\\w']+";
30th Jan 2022, 9:39 PM
zemiak