-
November 7th, 2011, 07:14 AM
#1
talkative
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=null;
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){System.out.println("\n\n\n Problem at creating existent file components - has next loop \n\n\n");}
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){System.out.println("\n\n\n Problem at creating existent file components - reacher \n\n\n");}
while(i2<rand){
try{ answer= buffer1.readLine();}catch(Exception e){System.out.println("\n\n\n Problem at reading line from existent file \n\n\n");}
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(fquestion);
questionbuffer=new BufferedReader(new InputStreamReader(dquestion));
}catch(Exception e){System.out.println("\n\n\n Problem at creating resource file components \n\n\n");}System.out.print("reached");
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("reached");
squestion.close();
try{squestion=new Scanner("C:\\talkative\\resources\\resourcequestions.txt");
fquestion=new FileInputStream("C:\\talkative\\resources\\resourcequestions.txt");
dquestion=new DataInputStream(fquestion);
questionbuffer=new BufferedReader(new InputStreamReader(dquestion));}catch(Exception e){System.out.println("\n\n\n Problem at creating resource file components \n\n\n");}
while(qi2<randq){
qi2++;
System.out.print("reached");
try{dontknowanswer=questionbuffer.readLine();
if(dontknowanswer=="")continue;
}catch(Exception e){System.out.println("\n\n\n Problem at reading resource file line \n\n\n");}
}
squestion.close();
guiob.settextgui(dontknowanswer);
}
Formatter f;
try{
f=new Formatter(location);
}catch(Exception e){System.out.println("\n\n\n Problem at creating formatter \n\n\n");}
}
}
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",20);
public JTextField talkativetext=new JTextField(20);
public String textset="";
public void settextgui(String s){
s+=textset;
System.out.println("\nthis is the string"+s);
talkativetext.setText(textset);
}
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);
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());
}
}
}
}
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);
}
}
Last edited by cens; November 10th, 2011 at 01:35 PM.
Reason: :D
-
November 9th, 2011, 09:34 AM
#2
Re: talkative
Please use code tags when posting code.
Also in what way does it not seem to work, are there any compile time or run time errors or does the field just not update?
-
November 10th, 2011, 08:11 AM
#3
Re: talkative
the field bassically doesent update.. The program is clockwork excepting this
-
November 10th, 2011, 10:32 AM
#4
Re: talkative
Have you tried adding a print statement in the settext method so you can see what text your code is trying to output?
-
November 10th, 2011, 10:37 AM
#5
Re: talkative
yep, the arguments are passed as it should, only the field is not modifing
-
November 10th, 2011, 10:39 AM
#6
Re: talkative
the weird thing is that in the first case, when the file exists the text is modified. Only when the text does not exist the field is not updateing
-
November 10th, 2011, 11:15 AM
#7
Re: talkative
If you do as I previously asked and post the code in code tags (with each class in their own code tags) I'll look at the code.
But if it's isn't working when there isn't a file it's probably because you are catching exceptions and then ignoring them - There are very few occasions when this is a good idea. At the very least print the exception stack trace to the console so you can see an exception has been thrown and from where.
BTW you should use Java coding standards as it makes it a lot easier for you and others to read your code. For example: Class names start with an upper case letter, method names and variables start with a lower case letter and concatenated words have an uppercase letter where they join ie settext should be setText etc
-
November 10th, 2011, 12:37 PM
#8
Re: talkative
updated. Thank you a lot keang, I havent found someone to answer so fast and so goodly. I'll try to use the standards next time Right now im checking my exceptions
-
November 10th, 2011, 12:55 PM
#9
Re: talkative
O..M..G.. not a single exception
-
November 10th, 2011, 01:12 PM
#10
Re: talkative
The code you have posted won't even run.
Your file class creates an instance of the gui class and the gui class creates and instance of the file class so you have a circular dependency which results in a stack overflow.
-
November 10th, 2011, 01:33 PM
#11
Re: talkative
oops..sorry...older version posted
-
November 10th, 2011, 01:35 PM
#12
Re: talkative
updated with today's code
-
November 10th, 2011, 01:56 PM
#13
Re: talkative
O..M..G.. not a single exception
When I run it without a file I get a FileNotFoundException at line 194 of the file class. Of course it would have helped you if you had done what I suggested and printed out the exception's stack trace rather than your own message.
-
November 10th, 2011, 02:04 PM
#14
Re: talkative
um? im not quite a master at exceptions. I know the minimal. Also, you must create the directory talkative in your c drive containing a resource folder. Inside the resource folder you need a text file called resourcequestions.txt Thank you keang i really appreciate you for dedicating time to me
-
November 10th, 2011, 02:07 PM
#15
Re: talkative
oh yes, also, it's not so self caring. It cannot create resource folders by himself yet. I'll make such an option later on
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|