Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

[JAVA] if


  • Please log in to reply

#1
Nathan

Nathan

    Member

  • Member
  • PipPipPip
  • 691 posts
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?

Edited by Nathan, 16 February 2012 - 03:30 PM.

  • 0

Advertisements


#2
havredave

havredave

    GeekU Moderator

  • GeekU Moderator
  • 1,711 posts
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. ;)
  • 0

#3
Nathan

Nathan

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 691 posts
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:
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP