0

I am fairly new to Java and was trying to make a username check for profanities. I have made an Array with 4 profanities and now I wanted to check the user's input for the bad words, however, I don't know how to form the if statement to check all items from the array.

    public static void main(String[] args) {

    Scanner character = new Scanner(System.in);

    String[] profanities = {"asshole", "ass", "idiot", "stupid"};


    System.out.println("What is your name");
    String userName = character.next();
    if (userName.contains(profanities[])) { //This Part is what i dont understand
        System.out.println("Invalid name");
    }
    else {
        System.out.println("Valid Name!");
    }
}
  • [`String::contains`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#contains(java.lang.CharSequence)) takes a `CharSequence` as argument. The expression `profanities[]` is not a legal expression in java. I recommend reading a basic tutorial on Java, e.g. [this one by Oracle](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/). – Turing85 Dec 23 '20 at 17:47
  • Since your `profanities` array has both `"asshole"` and `"ass"`, you're not trying to do a substring search, aka a `String.contains()` check, which means you're actually trying to do `profanities.contains(userName)`, i.e. the user name is not allowed to be one of those 4 words. Is that correct? – Andreas Dec 23 '20 at 17:48
  • @AdrianB you first need to think about what you're trying to do, before doing it. Should username `asshole dan` be allowed, or not? What about `idiotjane`? If you say 'it should not be' for both, then `asshole` is useless, as `ass` already covers that. Also, that means `LassieJane` is an invalid name, and thus you picked a bad algorithm. `a$$hole` would be valid, though. You see where I'm going with this, hopefully. 'filter for profanity' is in fact very difficult to do right. – rzwitserloot Dec 23 '20 at 17:58
  • The poor Queen bot is going into a frenzy over this post – Hovercraft Full Of Eels Dec 23 '20 at 18:04
  • 2
    Poor "Cassandra" :-( – dnault Dec 23 '20 at 18:07
  • I'm tempted to vote to close. On the surface this is straightforward, but there are indeed some unanswered questions about how strict this has to be that don't exist in the original question. Are you going for the [clbuttic](https://en.wikipedia.org/wiki/Scunthorpe_problem) implementation which would mean checking substrings, or are you looking to match exactly? What test data do you have that you expect to be invalid names, and what do you have to be valid names? – Makoto Dec 23 '20 at 18:11
  • Cassandra is one of many examples why profanity filters are incredibly stupid. Human names alone are so varied that you're gonna get more false positives with this use than true positives. Honestly, you're better off manually cleaning up bad names than trying to filter them out in advance, at least for real use cases. Also, since it's a username, someone might think about "UnknownClassification" - cl**ass**ification. I can think of hundreds of examples where this just doesn't work. This not being the point aside, please consider not doing this at all and do something better about the problem – Zoe Dec 23 '20 at 18:29

5 Answers5

0

Use a Set instead of an ArrayList and then profanities.contains(userName). Mind you, the user should have inputted the exact profanity as in the profanities Set, in order for the if statement to evaluate to true. If the user inputs something like 'userjackass', it will not be classed as profanity.

The Riddler
  • 107
  • 8
0

Without the need of creating further Collections, just your original array. As the set of invalid names would be previously set/known, the sort operation could only be performed once, when the program is started.

Once sorted, just call binarySearch on it for every input:

Arrays.sort(profanities); //--> if profanities is a static set, call this just once.

if (Arrays.binarySearch(profanities,username)>=0)  
    System.out.println("Invalid name");
else 
    System.out.println("Valid Name!");

//binarySearch will return >=0 if the value is found

This avoids the creation of sets, lists, or the implementation of loops.

If the set of invalid names may change (adding new ones, for example), this would require a second sort operation. In this scenario, the other answers provided would be a better approach. Use this method if the invalid names set is known and won't be altered during your program's execution.

aran
  • 10,978
  • 5
  • 39
  • 69
0

I would do it like this..

import java.util.Scanner;

public class Profanities {

    public static void main(String[] args) {

        boolean allow = false;
        Scanner scan = new Scanner(System.in);
        String[] profanity_list = {"asshole", "ass", "idiot", "stupid"};
        String test_input = "";

        // do while until allow equals true..
        while (!allow) { 

            System.out.println("What is your name?");
            test_input = scan.nextLine();
            byte memory = 0;

            // compare the input with the elements of the array..
            for (int pos = 0; pos < profanity_list.length; pos++) {

                if (test_input.contains(profanity_list[pos]) == true) {
                    System.out.println("Invalid name. Let's start again..");
                    memory = 1;
                    break;
                }
            }
            // if memory equals 1, it means that a profanity was found..
            if(memory == 0){allow = true;}
        }
        String name = test_input;
        System.out.println("That is a valid name. Thanks.");
    }
}
Gass
  • 7,536
  • 3
  • 37
  • 41
-1

Try:

for(String string : profanities)
  if(userName.contains(string))System.out.println("invalid name"); 
  else System.out.println("valid name");

Note: the for loop iterates through every entry in the array and checks to see if the entry is contained within userName

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
i_o
  • 777
  • 9
  • 25
  • please explain how this code solve the problem – OMR Dec 23 '20 at 18:09
  • @OMR the for loop iterates through every entry in the array and checks to see if the entry is contained within userName – i_o Dec 23 '20 at 18:27
  • @ArvindKumarAvinash I'm not a programmer working in industry. I do it for the fun of it. What is the guideline to follow for indentation in industry? – i_o Dec 23 '20 at 18:28
  • If you are using an IDE, it's just a matter of pressing a combination of keys e.g. in Eclipse, you can use Command + Shift + F (Mac) or Ctrl + Shift + F (Windows). – Arvind Kumar Avinash Dec 23 '20 at 18:37
-1

Instead of using Array of String, use List of String from which you can easily use contains method. I modified you're code with List, refer below

public static void main(String[] args) {

    Scanner character = new Scanner(System.in);

    //String[] profanities = {"asshole", "ass", "idiot", "stupid"};
    
    ArrayList<String> profanities = new ArrayList<String>();
    profanities.add("asshole");
    profanities.add("ass");


    System.out.println("What is your name");
    String userName = character.next();
    if (profanities.contains(userName)) { //This Part is what i dont understand
        System.out.println("Invalid name");
    }
    else {
        System.out.println("Valid Name!");
    }
}

One more advantage of using List is, it's dynamic, you can add elements to it in future without any issues

B Sangappa
  • 574
  • 4
  • 8