Talkative

classic Classic list List threaded Threaded
1 message Options
cens cens
Reply | Threaded
Open this post in threaded view
|

Talkative

Salut, sunt nou pe forum:D Am venit aici pentru a cere ajutor cu privire la un proiect pe care il dezvolt. Este o varianta mai stupida a celebrului clever bot; Voi adauga codul mai jos, cu toate cele 3 clase;


file class
Code:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

import javax.swing.JOptionPane;
public class file {
    //objects
    gui guiob=new gui();
   
   
   
   
   
    int i=0;//for counting
    int i2=0;//reacher
    String answer;
    String testblanc;
    public int rand;
    public Scanner scan;
    FileInputStream fstream;
    DataInputStream dstream;
    BufferedReader buffer;
    Scanner scan1;
    FileInputStream fstream1;
    DataInputStream dstream1;
    BufferedReader buffer1;
    //requestion vars
    FileInputStream fquestion;
    DataInputStream dquestion;
    BufferedReader questionbuffer;
    Scanner squestion;
    File fiquestion=new File("C:\\talkative\\resources\\resourcequestions.txt");
     int qi=0;
    String blanccheck;
    int randq;
    int qi2=0;
    String dontknowanswer;
   
   
    public void getAnswer(String s){
        String location="C:\\talkative\\resources\\";
        location+=s;
        location+=".txt";
        File x=new File(location);
        if(x.exists()){
        //has next loop
             try{
                scan=new Scanner(new File(location));
                fstream=new FileInputStream(location);
                dstream=new DataInputStream(fstream);
                buffer=new BufferedReader(new InputStreamReader(dstream));
             }catch(Exception e){}
            while(scan.hasNextLine()==true){
                i++;
                testblanc=scan.nextLine();
                if(testblanc=="")continue;
               
            }
           
           
            //chooser
      Random random=new Random();
      rand=1+random.nextInt(i);
      System.out.print(rand);
      //reacher
      try{fstream1=new FileInputStream(location);
      dstream1=new DataInputStream(fstream1);
      buffer1=new BufferedReader(new InputStreamReader(dstream1));
     
      }catch(Exception e){}
      while(i2<rand){
        try{ answer= buffer1.readLine();}catch(Exception e){}
          i2++;
          if(answer=="")continue;
      }
      i2=0;
      scan.close();
        }
        else{
            //readers
            try{
                squestion=new Scanner(new File("C:\\talkative\\resources\\resourcequestions.txt"));
                fquestion=new FileInputStream("C:\\talkative\\resources\\resourcequestions.txt");
                dquestion=new DataInputStream(fstream);
                questionbuffer=new BufferedReader(new InputStreamReader(dstream));
             }catch(Exception e){}
            if(!fiquestion.exists()){JOptionPane.showMessageDialog(null,"ERROR!","Missing file , resourcequestions.txt",JOptionPane.ERROR_MESSAGE);}
            else{
                while(squestion.hasNext()){
                    qi++;
                   
                    blanccheck=squestion.nextLine();
                    if(blanccheck=="")continue;
                   
                }
               
                Random randqr=new Random();
                randq=1+randqr.nextInt(qi);
                System.out.print(randq);
                squestion.close();
                try{squestion=new Scanner("C:\\talkative\\resources\\resourcequestions.txt");
                fquestion=new FileInputStream("C:\\talkative\\resources\\resourcequestions.txt");
                dquestion=new DataInputStream(fstream);
                questionbuffer=new BufferedReader(new InputStreamReader(dstream));}catch(Exception e){}
               
                while(qi2<randq){
                    System.out.print("reached");
                    try{dontknowanswer=questionbuffer.readLine();
                    if(dontknowanswer=="")continue;
                    qi2++;
                   
                    }catch(Exception e){}
                }
               
                squestion.close();
                guiob.settext(dontknowanswer);
               
               
               
               
            }
           
           
           
           
           
            Formatter f;
             try{
                 f=new Formatter(location);
             }catch(Exception e){}
            scan.close();
        }
       
       
       
    }
public String returnAnswer(){
    return answer;
   
}

}





gui class

Code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class gui extends JFrame{
public JTextField usertext=new JTextField("input here",40);
public JTextField talkativetext=new JTextField(40);


public gui(){

    //gui
    super("talkative");
setLayout(new FlowLayout());
add(usertext);
add(Box.createHorizontalStrut(80000));
add(Box.createHorizontalStrut(80000));
add(Box.createHorizontalStrut(80000));
add(Box.createHorizontalStrut(80000));
add(talkativetext);
talkativetext.setEditable(false);

listener ob=new listener();
usertext.addActionListener(ob);


//constructors for other classes









}public class listener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //constructors
        file objectfile=new file();
        //brain
        if(e.getSource()==usertext){
            String usertextget=String.format(""+e.getActionCommand());
            objectfile.getAnswer(usertextget);
            talkativetext.setText(objectfile.returnAnswer());
        }
       
       
       
       
    }
   
   
}
public void settext(String s){
    talkativetext.setText(s);
}

}



main class

Code:
import javax.swing.*;
public class main {
public static void main(String args[]){
    file ob=new file();
    gui obgui =new gui();
obgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obgui.setVisible(true);
obgui.setSize(400,400);
   
}
}






problematic lines


Code:
public void settext(String s){
    talkativetext.setText(s);
}



aceste linii nu functioneaza corect: String s este primit dar textul nu este setat;
Puteti gasi codul in ultima parte a classului gui