Looping through words in a message

Discussion in 'Plugin Development' started by RoboticPlayer, Oct 12, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    RoboticPlayer

    I am trying to loop through all of the words in a message within an AsyncPlayerChatEvent, but for some reason it is only getting the first word. Here is my loop along with check:
    Code:java
    1. for (String word : e.getMessage().split(" ")) {
    2. if (!(word.contains("train") || word.contains("trian"))) {
    3. System.out.println("Message doesn't have train or trian");
    4. return;
    5. }
    6. // I have other stuff here but it's not relevant
    7. }

    When I type "trains are fun" or anything with "train" or "trian" in the first word, it works fine. But if I say "who like trains?" or something with "train"/"trian" in a word that isn't the first, it isn't working. By "it isn't working" I mean it is saying that the message doesn't have train or trian. Side-note: I do realize that the message is case-sensitive right now, does anyone know how to fix this?
     
  2. Offline

    Zombie_Striker

    @henderry2019
    Why are you splitting the words up? Why not use e.getMessage().contians (Trains)

    Also, it should have been obvious that you're checking if it does or does not have the word train in it, and that by using "return" it will not continue looping through that string.

    If you wrote it, you made a major flub. If you got this from someone else, DON'T COPY AND PASTE.
     
    OhhPickles likes this.
  3. Offline

    boomboompower

    The great spelling be real xD
    -Grammer Nazi
     
    Zombie_Striker likes this.
  4. @Zombie_Striker actually he checks of none of both is contained
    Code:
    if(!(condition1 || condition2)){}
    Besides that you are totally right. He schould rather check
    Code:
    if(message.contains("train") || message.contains("trian")){
    // execute stuff when it is contained
    } else{
    // message doesnt have train or trian in it
    }
    This way there is also no need to force the method to stop by a return
     
  5. Offline

    DoggyCode™

    Anyways, a string like "Who likes trains?" Wouldn't work, because "Trains" is a different string from "Trains?"
     
  6. Offline

    Xerox262

    Yes it would, he's using String#contains() not String#equals(). You did do a capital T though, and he's only checking for lower case t's so yea, it wouldn't work, but not because Trains has an s, he just has to convert the string to lower case and it would work if someone typed "train" or even if they typed "teamtankanlkteaTRAINeaklmteamnklt"

    The thing he asked was why it only scanned the first word, it's because he's got a return when a word isn't train, remove the return and see what that's like for you, then try find it out from there
     
  7. Offline

    RoboticPlayer

    So I need to remove my return statement and do an normal check (instead of inverted) if the word contains train? And then for capitalization could I just do word.toLowerCase(); then check it?
     
  8. Offline

    Xerox262

    Yes, but if you're looking for efficiency you should use getMessage().toLowerCase().contains() because while what you have will work, it will not tell if the whole message contains the word, just if a specific word within the message contains the word.
     
  9. Offline

    RoboticPlayer

    @Xerox262 So I wouldn't actually need to loop through the words at all?
     
  10. Offline

    Xerox262

    Not unless you plan on replacing the word, you could just use String#replace for that though...
     
  11. Offline

    Zombie_Striker

    @Shmobi
    Actually no, he is checking if the first condition is false (if the word is not trains), and then he checks if that condition (which is exactly the same), is true.

    Gotta get those precious nanoseconds!

    It would look cleaner and would do exactly the same thing if you removed the loop.

    And again, @henderry2019 , How did you end up with a return; in there? That's the only question I have for you.
     
  12. Offline

    Xerox262

    I was more talking about if he needed to do it multiple times, or if he needed to come back to it in the future would it be better to have something span multiple lines when it can be done in a single line
     
  13. Offline

    RoboticPlayer

    First one: nope. Look at the parenthesis. Also, I am checking for the words "train" and "trian" not both "train"
    As for the return statment, I didn't want to do anything if it didn't contain "train" so I returned, not think that it would just stop the loop after the first word.
     
  14. @henderry2019 That code does this process.
    A message is entered "I like trains"
    It loops through each word
    I
    does't contain, so return.
    for loop ended.

    Just check if the message contains or doesn't contain the words.
     
Thread Status:
Not open for further replies.

Share This Page