Pattern pattern = Pattern.compile("\\p{P}\\S");
String[] tests = new String[] {
"Hi, my name is Tom Cruise. I like movies",
"Hi,my name is Tom Cruise. I like movies",
"Hi,my name is Tom Cruise.I like movies"
};
int[] results = new int[] { 0, 0, 0 };
for (int i = 0; i < tests.length; i++) {
Matcher matcher = pattern.matcher(tests[i]);
while(matcher.find()) {
results[i] += 1;
}
if (results[i] == 0) {
System.out.println("Sentence " + (i + 1) + " is perfect");
} else if (results[i] > 1 && results[i] < 3) {
System.out.println("Sentence " + (i + 1) + " is good");
} else {
System.out.println("Sentence " + (i + 1) + " is bad");
}
}
// now you know how many violations there were on every line.
// do whatever you want with them.