How can I run this? 11-13-2015, 06:45 PM
#1
So I've made this program (chatbot) in Notepad++ and I don't really know how to run it.
Here it is if you wanna see the code:
I have JDK installed and everything is set up but I don't know how to run it.
Here it is if you wanna see the code:
Code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements KeyListener{
JPanel p=new JPanel();
JTextArea dialog=new JTextArea(20,50);
JTextArea input=new JTextArea(1,50);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
String[][] chatBot={
//standard greetings
{"zdravo","bok","cao","hej"},
{"Hej!","Zdravo!","Hej!"},
//question greetings
{"kako si","kak si"},
{"Dobro sam","Super sam!"},
//yes
{"da"},
{"ne","NE","NE!!!!!!!"},
//talk about music
{"kakvu muziku slusas","kakvu glazbu volis","kakvu muziku volis","kakvu glazbu slusas"}
{"slusam rock","obozavam rock&roll","fan sam rock&roll-a"}
//sports
{"kakve sportove volis","koje sportove igras","koje sportove volis"}
{"igram nogomet, košarku, odbojku, rukomet, baseball, rugby i hokej"}
//default
{"zašuti","loš si","isuse","ajde, prestani pricati",
"(Tinov Prijatelj nije dostupan zato što bari žene)"}
};
public static void main(String[] args){
new ChatBot();
}
public ChatBot(){
super("Chat Bot");
setSize(600,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
input.addKeyListener(this);
p.add(scroll);
p.add(input);
p.setBackground(new Color(255,200,0));
add(p);
setVisible(true);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(false);
//-----grab quote-----------
String quote=input.getText();
input.setText("");
addText("-->Alen:\t"+quote);
quote.trim();
while(
quote.charAt(quote.length()-1)=='!' ||
quote.charAt(quote.length()-1)=='.' ||
quote.charAt(quote.length()-1)=='?'
){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
/*
0:we're searching through chatBot[][] for matches
1:we didn't find anything
2:we did find something
*/
//-----check for matches----
int j=0;//which group we're checking
while(response==0){
if(inArray(quote.toLowerCase(),chatBot[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length);
addText("\n-->Tinov Prijatelj\t"+chatBot[(j*2)+1][r]);
}
j++;
if(j*2==chatBot.length-1 && response==0){
response=1;
}
}
//-----default--------------
if(response==1){
int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length);
addText("\n-->Tinov Prijatelj\t"+chatBot[chatBot.length-1][r]);
}
addText("\n");
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(true);
}
}
public void keyTyped(KeyEvent e){}
public void addText(String str){
dialog.setText(dialog.getText()+str);
}
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}I have JDK installed and everything is set up but I don't know how to run it.
![[Image: 200_s.gif]](https://media1.giphy.com/media/UgQ3BSsv5Xuc8/200_s.gif)



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)























![[Image: 7ajmN5P.jpg]](https://i.imgur.com/7ajmN5P.jpg)