2 Replies Latest reply: Apr 16, 2007 7:53 PM by 807596 RSS

    New to Java...Making a "Stupid" Bot

    807596
      I'm new to these forums, so if this is the wrong place, please move it. Heh, kinda stupid bot 'cause I haven't done like anything, he's just a command line bot, but for some reason, I always get "I don't understand: Hey" or whatever I type. Here is the code:
      import java.io.*;
      
      public class test {
           public static void main(String[] args) throws IOException {
                InputStreamReader isr;
                BufferedReader input;
                String userentry;
                isr = new InputStreamReader(System.in);
                input = new BufferedReader(isr);
                System.out.println("Please type something and then hit enter!");
                userentry = input.readLine();
                if(userentry=="Hey") {
                     System.out.println("Hey right back at you!");
                } else if(userentry=="Sup?") {
                     System.out.println("Not a lot, how 'bout you?");
                } else {
                     System.out.println("I don't understand: " + userentry);
                }
           }
      }
        • 1. Re: New to Java...Making a "Stupid" Bot
          807596
          I'm new to these forums, so if this is the wrong
          place, please move it. Heh, kinda stupid bot 'cause
          I haven't done like anything, he's just a command
          line bot, but for some reason, I always get "I don't
          understand: Hey" or whatever I type. Here is the
          code:
          This is a classic String class gotcha. Compare Strings (and most everything else except primitive types) with the equals method, not "==".

          thus:
                    userentry = input.readLine();
                    if(userentry.equals("Hey")) {
                         System.out.println("Hey right back at you!");
                    } else if(userentry.equals("Sup?")) {
                         System.out.println("Not a lot, how 'bout you?");
                    } else {
                         System.out.println("I don't understand: " + userentry);
                    }
          You'll find it works now.

          Good luck, and happy coding!

          /Pete
          • 2. Re: New to Java...Making a "Stupid" Bot
            807596
            Yea, I found that out, thanks for the help though, 10 duke stars awarded. :)