Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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