简体   繁体   English

字符串列表-奇怪的行为

[英]List of Strings - Odd Behavior

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. 我尝试了所有我能想到的探索,但没有找到任何方法来理解我的期望与所得之间的差异。

When using UNIX line endings ( \\n ) in your text file, your program produces the output you expect. 在文本文件中使用UNIX行尾( \\n )时,程序将产生预期的输出。 If however you use dos line endings, you (almost) get the output you describe. 但是,如果您使用dos行结尾,则(几乎)获得您描述的输出。 The true output I see is: 我看到的真实输出是:

words
 otestingo
for
 otestingo
testing
 otestingo
false

You're probably not on a UNIX derivative OS - and I don't know what a Windows tool to convert line endings is - but if you have Vim you can use the command ff=unix and write the file back to change the line endings. 您可能不在UNIX派生操作系统上-并且我不知道Windows转换行尾的工具是什么-但如果您具有Vim,则可以使用命令ff=unix并写回文件以更改行尾。

Alternatively, you can simply remove this line: 或者,您可以简单地删除以下行:

fileScanner.useDelimiter("\\n");

... and the scanner will correctly deal with your dos line endings. ...,扫描仪将正确处理您的dos线尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM