简体   繁体   English

我在 Java 中的亵渎过滤器有什么问题?

[英]What's wrong with my profane filter in Java?

In this program, "cat", "dog", and "llama" are considered profane and needs to be filtered out of a sentence.在这个程序中,“cat”、“dog”和“llama”被认为是亵渎的,需要从一个句子中过滤掉。 Any version of those three words in CAPS("cAT"), extra letters("lLlama") needs to be considered as well.大写字母(“cAT”)、额外字母(“lLlama”)中这三个词的任何版本都需要考虑。 My program currently only reads one word at a time nor will it read capital/lowercase letters.我的程序目前一次只能读取一个单词,也不会读取大写/小写字母。 How can I add this?我怎样才能添加这个?

One run through:一通:

Enter a line to be checked for profanity:DoG CaT LlAmA |


Your input line 
 does not contain cat
 does not contain dog
 does not contain llama
This line would not be considered profane.

This is wrong, the input does contain the words and is considered profane.这是错误的,输入确实包含单词并且被认为是亵渎的。

My code:我的代码:

import java.util.Scanner;

public class ProfaneFilter
{
public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a line to be checked for profanity: ");
    String sentence;
    sentence = keyboard.next();

    System.out.println(" ");
    System.out.println("Your input line ");

    sentence = keyboard.nextLine();
    sentence.equalsIgnoreCase(sentence);

    if (sentence.equals("cat") && sentence.equals("dog") && sentence.equals("llama"))
        System.out.print("contains cat" + "\n" + "contains dog" + "\n" + "contains llama" + "\n" + "This line would be considered profane.");
    else if (!sentence.equals("cat") && !sentence.equals("dog") && !sentence.equals("llama"));
        System.out.println(" does not contain cat" + "\n" + " does not contain dog" + "\n" + " does not contain llama" + "\n" + "This line would not be considered profane.");

    if (sentence.equals("cat"))
        System.out.println("contains cat");

    else if (sentence.equals("dog"))
        System.out.println("contains dog");

    else if (sentence.equals("llama"))
        System.out.println("contains llama");
    }
}

I know I'm probably missing some important code to get the output I'm looking for, but I'm not sure what it is.我知道我可能缺少一些重要的代码来获得我正在寻找的输出,但我不确定它是什么。

You could get the output you need, using the contains instead of the equals, as the equals looks for an exact match.您可以获得所需的输出,使用 contains 而不是 equals,因为 equals 寻找完全匹配。 Also if you could cast the input string to the lower case, it may help.此外,如果您可以将输入字符串转换为小写,它可能会有所帮助。

Please try running the code below and see if it works.请尝试运行下面的代码,看看它是否有效。 Also feel free to replace the if else with switch case if you feel its more readable.如果您觉得它更具可读性,也可以随意将 if else 替换为 switch case。

package details;

import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

public class ProfaneFilter
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a line to be checked for profanity: ");
String sentence;
sentence = keyboard.next();

System.out.println(" ");
System.out.println("Your input line ");

sentence = keyboard.nextLine().toLowerCase();

StringBuffer stringBuffer = new StringBuffer();
if(sentence.contains("cat")){
    stringBuffer.append("The sentence contains cat \n");
}else{
    stringBuffer.append("The sentence doesnt contains cat \n");
}
if(sentence.contains("dog")){
    stringBuffer.append("The sentence contains dog \n");
}else{
    stringBuffer.append("The sentence doesnt contains dog \n");
}
if(sentence.contains("llama")){
    stringBuffer.append("The sentence contains llama \n");
}else{
    stringBuffer.append("The sentence doesnt contains llama \n");
}
System.out.println("stringBuffer is "+stringBuffer.toString());

} }

} }

Here's how I would write it.这是我将如何编写它。 This would use an array of your profane words, and iterate across them to see if the sentence contains them.这将使用一组您的亵渎词,并遍历它们以查看句子是否包含它们。

import java.util.Scanner;

public class PF {
    public PF() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a line to be checked for profanity: ");
        String sentence = keyboard.nextLine();
        System.out.println("\nYour input line: " + sentence);

        String [] prof = new String []{"cat", "dog", "llama"};
        for (String s : prof)
        {
            if (sentence.toLowerCase().contains(s))
                System.out.println("The sentence contains " + s);
            else 
                System.out.println("The sentence does not contain " + s);
        }       
    }
    public static void main(String...banana) { new PF();}
}

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

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