-5

When I make an array of strings the equalsIgnorecase doesn't recognize it, in eclipse it's suggesting me to change the array to something else, vice verca.

  public void onChat(PlayerChatEvent event) {
      Player player = event.getPlayer();
      String[] curse = new String[] {"rude1", "rude2", "rudeN"};
      if (event.getMessage().equalsIgnoreCase(curse)) {
        event.setCancelled(true);
        player.sendMessage("Don't Swear");
     }
 }
}

It's my curse filter for my video game. What am I doing wrong?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hayden Taylor
  • 329
  • 1
  • 4
  • 9
  • Heed the eclipse error :P – smk Feb 20 '13 at 04:56
  • 1
    L33t speak isn't caught by those filters, for starters. – Makoto Feb 20 '13 at 04:56
  • equalsIgnoreCase takes in a String, not an array of string. You have to iterate through the array if you want to do the checking. See http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String) – Jason Yeo Feb 20 '13 at 04:57
  • @JasonYeo: At least link to Java 6 APIs when you do that...Java 1.4.2 is so old school... :P – Makoto Feb 20 '13 at 04:57
  • @Makoto oops, I totally missed the version number. Anyway, here's the link to it, http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String). – Jason Yeo Feb 20 '13 at 04:59

5 Answers5

1

You would need to do a loop through curses to check each one.

for (String c : curse) {
  ... do check ...
}
user2089674
  • 2,028
  • 2
  • 15
  • 7
1

Continue with what user2089674 suggested.

I guess you want to filter out messages containing curse words, so instead of using equalsIgnoreCase, use contains instead?

That is

for (String c : curse) {

  if(event.getMessage().contains(c)){
       ......punish bad players..
     }
}

sarahTheButterFly
  • 1,894
  • 3
  • 22
  • 36
1

It's been stated before, but the idea is that the method signature for String.equalsIgnoreCase() does not accept a String[] as a parameter.

Looking at it a bit closer - You may have a String[], but that isn't the same as a String. Think of it as you needing a $1 bill to use the vending machine, but you only have a $20 - it ain't going to work.

Hence, you are required to iterate over all elements of that array and feed them through equalsIgnoreCase.

Here's three ways to do it:

  1. A simple for loop

    for(int i = 0; i < curse.length(); i++) {
        if(curse[i].equalsIgnoreCase(event.getMessage()) {
            // logic
        }
    }
    
  2. An enhanced for loop

    for(String badWord : curse) {
        if(badWord.equalsIgnoreCase(event.getMessage()) {
          // logic
        }
    }
    
  3. Converting the array to a List collection through Arrays.asList(T... a) and using an Iterator<String> to iterate over them

    List<String> badWords = Arrays.asList(curse);
    Iterator<String> badWordIterator = badWords.iterator();
    while(badWordIterator.hasNext()) {
        if(badWordIterator.next().equalsIgnoreCase(event.getMessage())) {
            // logic
        }
    }
    
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

instead of this if (event.getMessage().equalsIgnoreCase(curse)) you can use simple for loop

 for(int i=0;i<curse.length;i++){
    if (event.getMessage().equalsIgnoreCase(curse[i])){
        event.setCancelled(true);
    player.sendMessage("Don't Swear");
    }
 }

or you can use enhanced for loop to check the values

for(String s:curse){
  if (event.getMessage().equalsIgnoreCase(s)) {
    event.setCancelled(true);
    player.sendMessage("Don't Swear");
 }
}
kaysush
  • 4,797
  • 3
  • 27
  • 47
0

You can't do that with a String[]. Iterate through it (preferably with an enhanced for loop) like this:


    for (String curse : curses) {
    } 

and inside the loop put your check:

    for (String c : curse) {
        if (event.getMessage().equalsIgnoreCase(c)) {
            event.setCancelled(true);
            player.sendMessage("Don't Swear");
        }
    }

So you iterate through the string array (therefore you get a result for each item in the array) and then check if the iterated item is equal to the message sent. By the way, I recommend asking these questions at spigotmc.org, as stackoverflow is not welcome to new people.

LeopardL GD
  • 139
  • 13