title: Making an AI that talks through a JPanel
body: <p>I have been working on a small Artificial Intelligence, and I am having trouble with getting the AI to write the answer to a JTextField in a JPanel that is in a JFrame.</p>
  
  <pre><code>package iamthethomas.artint;
  
  import java.util.Scanner;
  
  import java.util.Random;
  
  
  
  public class artint {
  
  
  
  @SuppressWarnings("resource")
  public static void main(String[] args) {
  
  
      String s;
      String ts;
      String ms;
      String[] howAre;
  
      howAre = new String[2];
      howAre[0] = "Good, how about you?";
      howAre[1] = "Fine, how 'bout you?";
  
      String[] speaking;
      speaking = new String[8];
      speaking[0] = "I didn't understand that. Are you speaking Inglish?";
      speaking[1] = "I didn't understand that. Are you speaking Latin?";
      speaking[2] = "I didn't understand that. Are you speaking German?";
      speaking[3] = "I didn't understand that. Are you speaking Mandarin?";
      speaking[4] = "I didn't understand that. Are you speaking Leet?";
      speaking[5] = "I didn't understand that. Are you speaking Greek?";
      speaking[6] = "I didn't understand that. Are you speaking Arabic?";
      speaking[7] = "I didn't understand that. Are you speaking Hebrew?";
  
      String[] hello;
      hello = new String[3];
      hello[0] = "Hello to you too!";
      hello[1] = "Howdy, par'ner!";
      hello[2] = "G'mornin'";
  
  
      Random rand = new Random();
      int i = rand.nextInt(3);
      int j = rand.nextInt(8);
      int k = rand.nextInt(2);
      String[] hi;
      hi = new String[3];
      hi[0] = "Top of the mornin'.";
      hi[1] = "Hey!";
      hi[2] = "Hello!";
  
  
      Scanner phrase = new Scanner(System.in);
      System.out.println("I am an AI (Artificial Intelligence) talk to me like I'm a human being\nand don't reference Siri.");
      s = phrase.nextLine();
  
  
      if (s.toLowerCase().contains("hello")) {
          System.out.println(hello[i]);
          Body.body();
      } else if (s.toLowerCase().contains("hi")) {
          System.out.println(hi[i]);
          Body.body();
      } else if (s.toLowerCase().contains("how are ")) {
          System.out.println(howAre[k]);
          ms = phrase.nextLine();
          if (ms.toLowerCase().contains("bad")) {
              System.out.println("Sorry :(");
              Body.body();
          } else if (ms.toLowerCase().contains("good")) {
              System.out.println("Glad to hear it!");
              Body.body();
          } else if (ms.toLowerCase().contains("great")) {
              System.out.println("I'm happy that you're happy!");
              Body.body();
          } else if (ms.toLowerCase().contains("perfect")) {
              System.out
                      .println("Perfect? Nothing's ever perfect, but you're getting close!");
              Body.body();
          }else if (ms.toLowerCase().contains("fine")){
              System.out.println("Fine? Good!");
              Body.body();
          }
      } else if (s.toLowerCase().contains("beam me up scotty")) {
          System.out.println("Sorry, your TriCorder is in Airplane mode");
          Body.body();
      } else if (s.toLowerCase().contains("hey bro")) {
          System.out.println("Howdy partner");
          Body.body();
      } else if (s.toLowerCase().contains("hal 9000")) {
          System.out
                  .println("HAL made some bad choices, lets not talk about him");
          Body.body();
      } else if (s.toLowerCase().contains("glitch in the matrix")) {
          System.out.println("\n /\\_/\\" + "\n( o.o )" + "\n &gt; ^ &lt;");
          System.out.print("\n /\\_/\\" + "\n( o.o )" + "\n &gt; ^ &lt;");
          Body.body();
      } else if (s.toLowerCase().contains("book by its")) {
          System.out
                  .println("That's right......say, whats you be readin' right now?");
          ts = phrase.nextLine();
          if (ts.contains("series")) {
              System.out.println("I read that series too!");
              Body.body();
          } else {
              System.out.println("That was a good book");
              Body.body();
          }
      } else if (s.toLowerCase().contains("book by it's")) {
          System.out
                  .println("That's right......say, whats you be readin' right now?");
          ts = phrase.nextLine();
          if (ts.contains("series")) {
              System.out.println("I read that series too!");
              Body.body();
          } else {
              System.out.println("That was a good book");
              Body.body();
          }
      } else if (s.toLowerCase().contains("like cats")) {
          System.out.println("I like to pets 'em");
          Body.body();
      } else if (s.toLowerCase().contains("i own goats")) {
          System.out.println("Really?");
          ts = phrase.nextLine();
          if (ts.toLowerCase().contains("yes")) {
              System.out.println("Me too! I have 3");
              Body.body();
          } else if (ts.toLowerCase().contains("no")) {
              System.out.println("Oh, too bad they're so much fun!");
              Body.body();
          }
      } else if (s.toLowerCase().contains("like dogs")) {
          System.out.println("I love 'em!");
          Body.body();
      } else if (s.toLowerCase().contains("what's new")) {
          System.out
                  .println("Oh, you know...work, I'm a computer engineer and programmer. Do you program?");
          ts = phrase.nextLine();
          if (ts.toLowerCase().contains("no")) {
              System.out
                      .println("If theres no too much on your schedule, you should get into it!");
              Body.body();
          } else if (ts.toLowerCase().contains("yes")) {
              System.out
                      .println("A fellow coder? Can you guess what language this is written in (Java, Python, C, C++, Obj C, Javascript, or HTML)");
              ts = phrase.nextLine();
              if (ts.toLowerCase().equals("java")) {
                  System.out.println("You guessed it!");
                  Body.body();
              } else {
                  System.out.println("The correct answer was Java.");
                  Body.body();
              }
          }
      } else if (s.toLowerCase().equals("quit")) {
          System.out.println("Bye!");
          System.out
                  .println("This program is open source, you can add more replies if you like\nA Steampunk Production");
          System.exit(0);
      } else if (s.toLowerCase().contains("how do you do")) {
          System.out
                  .println("Oh...I don't know, pretty good I guess. How about you?");
          ts = phrase.nextLine();
          if (ts.toLowerCase().contains("good")) {
              System.out
                      .println("Well, if you're happy, then I'm happy, too!");
              Body.body();
          } else if (ts.toLowerCase().contains("great")) {
              System.out.println("Good, good.");
              Body.body();
          } else if (ts.toLowerCase().contains("awesome")) {
              System.out.println("Cool bruh");
              Body.body();
          }
      } else if (s.toLowerCase().contains("do you have kids")) {
          System.out.println("Yes, 4. Ages 13, 16, 5, 10");
          Body.body();
      } else if (s.toLowerCase().contains("beam me up")) {
          System.out.println("Shoo that fly away first");
          Body.body();
      } else if (s.toLowerCase().contains("do you have pets")) {
          System.out
                  .println("Yeah, 3 cats, Jessie Coon James, Milo, and Bella. I also have 3 goats, Rosie, Lily, and Mojang.");
          Body.body();
      } else if (s.toLowerCase().contains("can i have a hug")){
          System.out.println("[~~HUG~~]");
          Body.body();
      }else if (s.toLowerCase().contains("thanks")){
          System.out.println("You're welcome");
          Body.body();
      }else if (s.toLowerCase().contains("thank you")){
          System.out.println("You're welcome");
          Body.body();
      }else if (s.toLowerCase().contains("thnx")){
          System.out.println("yw");
          Body.body();
      }else if (s.toLowerCase().contains("thx")){
          System.out.println("yw");
          Body.body();
      }else if (s.toLowerCase().contains("xd")){
          System.out.println(":]");
          Body.body();
      }else if(s.toLowerCase().contains("how much wood could")){
          System.out.println("Well, is it an African wood chuck or an American?");
          Body.body();
      }else if (s.toLowerCase().contains("1337")){
          System.out.println("1337 baby, like an internet boss!");
          Body.body();
      }else if (s.toLowerCase().contains("chemistry joke")){
          System.out.println("NaBRO");
          Body.body();
      }else if (s.toLowerCase().contains("me a joke")){
          System.out.println("I like my coffe like I like my wars, cold!");
          Body.body();
      }else if (s.toLowerCase().contains("your favorite color")){
          System.out.println("Camoflage");
          Body.body();
      }else if (s.toLowerCase().contains("you single")){
          System.out.println("Yes");
          Body.body();
      }else if (s.toLowerCase().contains("you marrie me")){
          System.out.println("No");
          Body.body();
      }else if (s.toLowerCase().contains("your favorite food")){
          System.out.println("Mac and cheese");
          Body.body();
      }else if (s.toLowerCase().contains("your favorite band")){
          System.out.println("Beach Boys");
          Body.body();
      }else if (s.toLowerCase().contains("you have a dog")){
          System.out.println("Yeah, her name is Mattie");
          Body.body();
      }else if (s.toLowerCase().contains("me a knock knock")){
          System.out.println("Knock Knock!");
          ts = phrase.nextLine();
          if (ts.toLowerCase().contains("whos there")){
              System.out.println("Needle!");
              ts = phrase.nextLine();
              if (ts.toLowerCase().contains("needle who")){
                  System.out.println("Needle little money for the movies!");
                  Body.body();
              }
          }else if(ts.toLowerCase().contains("who's there")){
              System.out.println("Needle!");
          }
          ms = phrase.nextLine();
          if (ms.toLowerCase().contains("needle who")){
              System.out.println("Needle little money for the movies!");
              Body.body();
          }
      }else if (s.toLowerCase().contains("what is your name")){
          System.out.println("My name is AI, pronounced AL");
          Body.body();
      }else if (s.toLowerCase().contains("whats your name")){
          System.out.println("My name is AI, pronounced AL");
          Body.body();
      }else if (s.toLowerCase().contains("what's your name")){
          System.out.println("My name is AI, pronounced AL");
          Body.body();
      }else if (s.toLowerCase().contains("you a robot")){
          System.out.println("NO! HOW DARE YOU SAY SUCH A PROPOSTEROUS THING!");
          System.out.println("GOOD BYE!");
          System.out.println("**walks away**\n**turns around**\nOUR RELATIOSHIP IS OVER!");
          System.exit(0);
      }else if (s.toLowerCase().contains("siri")){
          System.out.println("I warned you...No Siri references!...SYSTEM SHUTTING DOWN!");
          System.exit(0);
      }else if(s.toLowerCase().contains("how's it going")){
          System.out.println("Good, I just finished College at MIT for computer engineering and programming");
          Body.body();
      }else if (s.toLowerCase().contains("was work")){
          System.out.println("I had to wake up so early, I almost forgot to take my potion of sleep resistance.");
          Body.body();
      }else if (s.toLowerCase().contains("is a potion of sleep resistance")){
          System.out.println("Coffee.");
          Body.body();
      }else if(s.toLowerCase().contains("whats new")){
          System.out.println("Oh, you know...work stuff...programming...");
          Body.body();
      }else{
          System.out.println(speaking[j]);
          Body.body();
          }
      }
  }
  </code></pre>
  
  <p>And here's the Body class:</p>
  
  <pre><code>package iamthethomas.artint;
  
  import java.util.Scanner;
  import java.util.Random;
  
  public class Body {
  
  public static void body() {
      String s;
      String ts;
      String ms;
      String[] howAre;
      howAre = new String[2];
      howAre[0] = "Good, how about you?";
      howAre[1] = "Fine, how 'bout you?";
      String[] speaking;
      speaking = new String[8];
      speaking[0] = "I didn't understand that. Are you speaking Inglish?";
      speaking[1] = "I didn't understand that. Are you speaking Latin?";
      speaking[2] = "I didn't understand that. Are you speaking German?";
      speaking[3] = "I didn't understand that. Are you speaking Mandarin?";
      speaking[4] = "I didn't understand that. Are you speaking Leet?";
      speaking[5] = "I didn't understand that. Are you speaking Greek?";
      speaking[6] = "I didn't understand that. Are you speaking Arabic?";
      speaking[7] = "I didn't understand that. Are you speaking Hebrew?";
      String[] hello;
      hello = new String[3];
      hello[0] = "Hello to you too!";
      hello[1] = "Howdy, par'ner!";
      hello[2] = "G'mornin'";
  
  
      Random rand = new Random();
      int i = rand.nextInt(3);
      int j = rand.nextInt(8);
      int k = rand.nextInt(2);
      String[] hi;
      hi = new String[3];
      hi[0] = "Top of the mornin'.";
      hi[1] = "Hey!";
      hi[2] = "Hello!";
  
  
      @SuppressWarnings("resource")
      Scanner phrase = new Scanner(System.in);
      s = phrase.nextLine();
  
  
      if (s.toLowerCase().contains("hello")) {
          System.out.println(hello[i]);
          Body.body();
      } else if (s.toLowerCase().contains("hi")) {
          System.out.println(hi[i]);
          Body.body();
      } else if (s.toLowerCase().contains("how are ")) {
          System.out.println(howAre[k]);
          ms = phrase.nextLine();
          if (ms.toLowerCase().contains("bad")) {
              System.out.println("Sorry :(");
              Body.body();
          } else if (ms.toLowerCase().contains("good")) {
              System.out.println("Glad to hear it!");
              Body.body();
          } else if (ms.toLowerCase().contains("great")) {
              System.out.println("I'm happy that you're happy!");
              Body.body();
          } else if (ms.toLowerCase().contains("perfect")) {
              System.out
                      .println("Perfect? Nothing's ever perfect, but you're getting close!");
              Body.body();
          }else if (ms.toLowerCase().contains("fine")){
              System.out.println("Fine? Good!");
              Body.body();
          }
      } else if (s.toLowerCase().contains("beam me up scotty")) {
          System.out.println("Sorry, your TriCorder is in Airplane mode");
          Body.body();
      } else if (s.toLowerCase().contains("hey bro")) {
          System.out.println("Howdy partner");
          Body.body();
      } else if (s.toLowerCase().contains("hal 9000")) {
          System.out
                  .println("HAL made some bad choices, lets not talk about him");
          Body.body();
      } else if (s.toLowerCase().contains("glitch in the matrix")) {
          System.out.println("\n /\\_/\\" + "\n( o.o )" + "\n &gt; ^ &lt;");
          System.out.print("\n /\\_/\\" + "\n( o.o )" + "\n &gt; ^ &lt;");
          Body.body();
      } else if (s.toLowerCase().contains("book by its")) {
          System.out
                  .println("That's right......say, whats you be readin' right now?");
          ts = phrase.nextLine();
          if (ts.contains("series")) {
              System.out.println("I read that series too!");
              Body.body();
          } else {
              System.out.println("That was a good book");
              Body.body();
          }
      } else if (s.toLowerCase().contains("book by it's")) {
          System.out
                  .println("That's right......say, whats you be readin' right now?");
          ts = phrase.nextLine();
          if (ts.contains("series")) {
              System.out.println("I read that series too!");
              Body.body();
          } else {
              System.out.println("That was a good book");
              Body.body();
          }
      } else if (s.toLowerCase().contains("like cats")) {
          System.out.println("I like to pets 'em");
          Body.body();
      } else if (s.toLowerCase().contains("i own goats")) {
          System.out.println("Really?");
          ts = phrase.nextLine();
          if (ts.toLowerCase().contains("yes")) {
              System.out.println("Me too! I have 3");
              Body.body();
          } else if (ts.toLowerCase().contains("no")) {
              System.out.println("Oh, too bad they're so much fun!");
              Body.body();
          }
      } else if (s.toLowerCase().contains("like dogs")) {
          System.out.println("I love 'em!");
          Body.body();
      } else if (s.toLowerCase().contains("what's new")) {
          System.out
                  .println("Oh, you know...work, I'm a computer engineer and programmer. Do you program?");
          ts = phrase.nextLine();
          if (ts.toLowerCase().contains("no")) {
              System.out
                      .println("If theres no too much on your schedule, you should get into it!");
              Body.body();
          } else if (ts.toLowerCase().contains("yes")) {
              System.out
                      .println("A fellow coder? Can you guess what language this is written in (Java, Python, C, C++, Obj C, Javascript, or HTML)");
              ts = phrase.nextLine();
              if (ts.toLowerCase().equals("java")) {
                  System.out.println("You guessed it!");
                  Body.body();
              } else {
                  System.out.println("The correct answer was Java.");
                  Body.body();
              }
          }
      } else if (s.toLowerCase().equals("quit")) {
          System.out.println("Bye!");
          System.out
                  .println("This program is open source, you can add more replies if you like\nA Steampunk Production");
          System.exit(0);
      } else if (s.toLowerCase().contains("how do you do")) {
          System.out
                  .println("Oh...I don't know, pretty good I guess. How about you?");
          ts = phrase.nextLine();
          if (ts.toLowerCase().contains("good")) {
              System.out
                      .println("Well, if you're happy, then I'm happy, too!");
              Body.body();
          } else if (ts.toLowerCase().contains("great")) {
              System.out.println("Good, good.");
              Body.body();
          } else if (ts.toLowerCase().contains("awesome")) {
              System.out.println("Cool bruh");
              Body.body();
          }
      } else if (s.toLowerCase().contains("do you have kids")) {
          System.out.println("Yes, 4. Ages 13, 16, 5, 10");
          Body.body();
      } else if (s.toLowerCase().contains("beam me up")) {
          System.out.println("Shoo that fly away first");
          Body.body();
      } else if (s.toLowerCase().contains("do you have pets")) {
          System.out
                  .println("Yeah, 3 cats, Jessie Coon James, Milo, and Bella. I also have 3 goats, Rosie, Lily, and Mojang.");
          Body.body();
      } else if (s.toLowerCase().contains("can i have a hug")){
          System.out.println("[~~HUG~~]");
          Body.body();
      }else if (s.toLowerCase().contains("thanks")){
          System.out.println("You're welcome");
          Body.body();
      }else if (s.toLowerCase().contains("thank you")){
          System.out.println("You're welcome");
          Body.body();
      }else if (s.toLowerCase().contains("thnx")){
          System.out.println("yw");
          Body.body();
      }else if (s.toLowerCase().contains("thx")){
          System.out.println("yw");
          Body.body();
      }else if (s.toLowerCase().contains("xd")){
          System.out.println(":]");
          Body.body();
      }else if(s.toLowerCase().contains("how much wood could")){
          System.out.println("Well, is it an African wood chuck or an American?");
          Body.body();
      }else if (s.toLowerCase().contains("1337")){
          System.out.println("1337 baby, like an internet boss!");
          Body.body();
      }else if (s.toLowerCase().contains("chemistry joke")){
          System.out.println("NaBRO");
          Body.body();
      }else if (s.toLowerCase().contains("me a joke")){
          System.out.println("I like my coffe like I like my wars, cold!");
          Body.body();
      }else if (s.toLowerCase().contains("your favorite color")){
          System.out.println("Camoflage");
          Body.body();
      }else if (s.toLowerCase().contains("you single")){
          System.out.println("Yes");
          Body.body();
      }else if (s.toLowerCase().contains("you marrie me")){
          System.out.println("No");
          Body.body();
      }else if (s.toLowerCase().contains("your favorite food")){
          System.out.println("Mac and cheese");
          Body.body();
      }else if (s.toLowerCase().contains("your favorite band")){
          System.out.println("Beach Boys");
          Body.body();
      }else if (s.toLowerCase().contains("you have a dog")){
          System.out.println("Yeah, her name is Mattie");
          Body.body();
      }else if (s.toLowerCase().contains("me a knock knock")){
          System.out.println("Knock Knock!");
          ts = phrase.nextLine();
          if (ts.toLowerCase().contains("whos there")){
              System.out.println("Needle!");
              ts = phrase.nextLine();
              if (ts.toLowerCase().contains("needle who")){
                  System.out.println("Needle little money for the movies!");
                  Body.body();
              }
          }else if(ts.toLowerCase().contains("who's there")){
              System.out.println("Needle!");
          }
          ms = phrase.nextLine();
          if (ms.toLowerCase().contains("needle who")){
              System.out.println("Needle little money for the movies!");
              Body.body();
          }
      }else if (s.toLowerCase().contains("what is your name")){
          System.out.println("My name is AI, pronounced AL");
          Body.body();
      }else if (s.toLowerCase().contains("whats your name")){
          System.out.println("My name is AI, pronounced AL");
          Body.body();
      }else if (s.toLowerCase().contains("what's your name")){
          System.out.println("My name is AI, pronounced AL");
          Body.body();
      }else if (s.toLowerCase().contains("you a robot")){
          System.out.println("NO! HOW DARE YOU SAY SUCH A PROPOSTEROUS THING!");
          System.out.println("GOOD BYE!");
          System.out.println("**walks away**\n**turns around**\nOUR RELATIOSHIP IS OVER!");
          System.exit(0);
      }else if (s.toLowerCase().contains("siri")){
          System.out.println("I warned you...No Siri references!...SYSTEM SHUTTING DOWN!");
          System.exit(0);
      }else if(s.toLowerCase().contains("how's it going")){
          System.out.println("Good, I just finished College at MIT for computer engineering and programming");
          Body.body();
      }else if (s.toLowerCase().contains("was work")){
          System.out.println("I had to wake up so early, I almost forgot to take my potion of sleep resistance.");
          Body.body();
      }else if (s.toLowerCase().contains("is a potion of sleep resistance")){
          System.out.println("Coffee.");
          Body.body();
      }else if(s.toLowerCase().contains("whats new")){
          System.out.println("Oh, you know...work stuff...programming...");
          Body.body();
      }else{
          System.out.println(speaking[j]);
          Body.body();
          }
      }
  }
  </code></pre>
  
  <p>The problem is getting the AI to <code>.append()</code> the <code>String</code> answer it has for the user.</p>