0

I am trying to create an IRC Bot, below is the code, however, when it runs, it will not detect "001" whatever I try, I'm running it on JRE6 and using JDK6. I'm developing it with Eclipse and using the debug feature to run it. I've tried with both "java.exe" and "javaw.exe". When running, debugex shows 001 is clearly there.

Here is my code:

package bot;
import java.io.*;
import java.net.*;
public class Main {
    static PrintWriter out = null;
    @SuppressWarnings("unused")
    public static void main(String[] args) {
        Socket sock = null;
        BufferedReader in = null;
        try {
            sock = new Socket("irc.jermc.co.cc",6667);
            in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            out = new PrintWriter(sock.getOutputStream(), true);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean sentconnect = false;
        while(sock.isConnected() == true){
            if(sentconnect == false){
                writeout("NICK GBot");
                writeout("USER GBot GBot GBot :GBot");
                sentconnect = true;
            }
            String data = "";
            try {
                data = in.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            data = data.trim();
            if(data != ""){
                System.out.println("[IN] " + data);
            }
            String[] ex;
            ex = data.split(" ");
            for(String debugex : ex){
                //System.out.println("DEBUGEX: " + debugex);
            }
            if(ex[1] == "001"){
                writeout("JOIN #minecraft");
            }
            try {
                Thread.sleep((long) 0.5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(ex[0] == "PING"){
                writeout("PONG " + ex[1]);
            }
        }
    }

    private static void writeout(String msg) {
        out.println(msg);
        System.out.println("[OUT] " + msg);
    }
}
rid
  • 61,078
  • 31
  • 152
  • 193
JohnHoulderUK
  • 639
  • 2
  • 8
  • 27

1 Answers1

2

String equality must be tested using String.equals(), not ==.

It should be:

        if(! data.isEmpty()){                     // rather than: data != ""
            System.out.println("[IN] " + data);
        }
        ...
        if(ex[1].equals("001")){                  // rather than: ex[1] == "001"
            writeout("JOIN #minecraft");
        }
        ...
        if(ex[0].equals("PING")){                 // rather than: ex[0] == "PING"
            writeout("PONG " + ex[1]);
        }

See How do I compare strings in Java.

Community
  • 1
  • 1
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78