[JAVA] if - Geeks to Go Forums

Jump to content

Log in Register Register Malware removal guide How it works

[JAVA] if

#1 Nathan

  • Group: Member
  • Posts: 690
  • Joined: 10-November 11

Posted 16 February 2012 - 03:29 PM

I'm coding a Java IRC bot with Pircbot (here) as a base and just adding on.

I want to be able to disconnect a bot from any room, but with only my nick.
This is the code I've got:
if (message.equalsIgnoreCase("mybot")) {
        	if (sender == "nathanlol"){
        		sendMessage(channel, sender + ": Ok, I will disconnect");
        	}
        	else {
        		sendMessage(channel, sender + "Nope"); 
        	}
        }


But in the channel, I'm getting this:
21:22 Nathanlol mybot
21:22 Nathanlol[eBot] NathanlolNope
21:22 Nathann mybot
21:22 Nathanlol[eBot] NathannNope

Notice I tried with two nicks, and they both give me the else message.

This is something else which came with the bot which works:
public void onMessage(String channel, String sender,
                       String login, String hostname, String message) {
        if (message.equalsIgnoreCase("time")) {
            String time = new java.util.Date().toString();
            sendMessage(channel, sender + ": The time is now " + time);
        }

21:25 Nathanlol time
21:25 Nathanlol[eBot] Nathanlol: The time is now Thu Feb 16 21:25:47 UTC 2012

Am I doing something wrong?

#2 havredave

  • Group: Malware Removal
  • Posts: 802
  • Joined: 25-March 10

Posted 17 February 2012 - 05:41 PM

Unless I'm mistaken, and I could be given how long it's been since I did any Java programming, the problem is with case sensitivity.

I noticed right away that your nick is Nathanlol, but your code is checking for nathanlol. The bot, however, is responding with the correct capitalization.

This:

if (sender == "nathanlol"){


Should be more like this:

 if (sender.equalsIgnoreCase("nathanlol")) {


However, I don't know if equalsIgnoreCase is a method of message (from message.equalsIgnoreCase, in the code snip that you said works) that has been overridden in Pircbot or if it's a built-in Java method. Or if method is even the right word (yes, it's been that long).

Oh, and you need a couple spaces to things don't read like NathanlolNope. ;)

Hopefully that at least gives you the hint you need, and I haven't made an idiot of myself. ;)

#3 Nathan

  • Group: Member
  • Posts: 690
  • Joined: 10-November 11

Posted 18 February 2012 - 05:39 AM

I should of updated this thread.

The code you gave did work.. but I found out not long after posting this when I had another play around with it.
Haha, yeah, I only added "NathanlolNope" so I could get the triggers working. ;)

Of course you didn't make an idiot of yourself.. haha. :thumbsup:

Thank you for your time anyways. :cool: :happy:

Share this topic: