I'm working on an obscenity filter for a random word generator so that it avoids certain words or phrases. The code is fairly simple so far and I'm using some test words to try it out, yet there is already a strange error occurring that makes absolutely no sense to me.
final List<String> obscene;
WordEngine(){
obscene = new ArrayList<>();
loadObscene();
System.out.println(isObscene("otestingo"));
}
void loadObscene(){
try {
InputStream configStream = Interactions.class.getResourceAsStream("obscene.txt");
Scanner fileScanner = new Scanner(configStream);
fileScanner.useDelimiter("\\n");
String nextWord;
while(fileScanner.hasNext()){
nextWord = fileScanner.next();
obscene.add(nextWord);
}
}catch(Exception e){
System.out.println(e);
}
//for(String obsceneIterator : obscene){ System.out.println(obsceneIterator); }
}
boolean isObscene(String word){
for (Iterator<String> it = obscene.iterator(); it.hasNext();) {
String nextObscene = it.next();
String test = nextObscene;
System.out.println(test);
System.out.println(test + " " + word);
if(word.contains(nextObscene)){
return true;
}
}
return false;
}
The text file contains:
words
for
testing
The output is:
words
otestingo
for
otestingo
testing
otestingo
false
The expected output would be:
words
words otestingo
for
for otestingo
testing
testing otestingo
true
Something about concatenating the string or accessing it is causing it to be deleted. I've tried every sort of probing that I can think of and am not finding any way to make sense of the discrepancy between what I expect and what I get.
Aucun commentaire:
Enregistrer un commentaire