Code
public static int stopCounter=0, noStopCounter=0;
public static void compareLineToStopWords(String line, String[] stopWords) {
//split the line up into words
String[] words = line.split("[\\W&&[^']]+|[\\d]+");
//use sentinel to break from the search when a match is found
Boolean sentinel = false;
for (int i = 0; i < words.length && !sentinel; i++) {
int min = 0, max = stopWords.length - 1;
while (max >= min && !sentinel) {
int mid = (max + min) / 2;
//if a match is found quit searching and increment the stopCounter
if (stopWords[mid].equalsIgnoreCase(words[i])) {
stopCounter++;
sentinel = true;
} else if (stopWords[mid].compareToIgnoreCase(words[i]) > 0) {
max = mid - 1;
} else {
min = mid + 1;
}
}
//if no match is found by the last word print the line and increment the noStopCounter
if (i == (words.length - 1) && max < min) {
noStopCounter++;
System.out.println("-" + line);
}
}
}
here you can see a method that searches an array for each word in a line and breaks out of the search entirely as soon as it finds a match and if it doesnt find a match it does some other stuff
i used a sentinel to show how you would have to do it without a labeled break
Code
//use binary search method to compare each word in the line against stopWords
public static void compareLineToStopWords(String line, String[] stopWords) {
//split the line up into words
String[] words = line.split("[\\W&&[^']]+|[\\d]+");
//use labeled break instead of sentinel
test: {
for (int i = 0; i < words.length; i++) {
int min = 0, max = stopWords.length - 1;
while (max >= min) {
int mid = (max + min) / 2;
//if a match is found quit searching and increment the stopCounter
if (stopWords[mid].equalsIgnoreCase(words[i])) {
stopCounter++;
break test;
} else if (stopWords[mid].compareToIgnoreCase(words[i]) > 0) {
max = mid - 1;
} else {
min = mid + 1;
}
}
}
//if no match is found by the last word print the line and increment the noStopCounter
noStopCounter++;
System.out.println("-" + line);
}
}
as you can see with the break you can jump out of the nested loop entirely
it simplifies the conditions in the loops because you no longer need the sentinel
also you don't need the nostop counter if statement because you would have broken out if the loop entirely if you found a word before the last word
regular breaks are unreliable and should be avoided but a labeled break allows more control over what you're doing
This post was edited by Ideophobe on Mar 26 2016 06:47am