-5

EDIT: Thank you, PakkuDon

instead of using "==" I must use ".Equals()"!


I'm trying to implement a chat-command system though I've encountered troubles in differentiating between standard chat and command chat..

Focusing on just one command for now, `highlight

the output whenever I type in `highlight is:

 highlight
`highlight

Here's my code:

    String cmd =  InMessage.message.substring(0, 10);
    System.out.println(cmd);
    System.out.println("`highlight");
    if( cmd == "`highlight" )
    {
       ... cancel chat packet and proces command
    }

and yet the if statement returns false.

What's going on here? Anything I've done is wrong?

P̲̳x͓L̳
  • 3,615
  • 3
  • 29
  • 37
user2486771
  • 1
  • 1
  • 4

1 Answers1

0

You have a number of problems. Firstly, that substring is going to throw an exception if the user types something that's less than 10 characters. Secondly, you're using == where equals would work better. But you don't need either of these things. You could just use

if (InMessage.message.startsWith("`highlight")) {
    // whatever
}
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110