0

Dear All
I am trying to learn JAVA with Eclipse.I have a class like Form.java and it has a form which contains 4 text fields.When I press the submit button(Register) I want my program to put every thing into a file(test.txt) line by line.
I tried to do something from my study book but it does not work.I can not see any input in the external file.
I would be very happy if anyone can help me to learn further
Thanks in advance

Here is the codes

public class Form extends JFrame implements ActionListener 
{
	String[] Reg = {"","","",""};
	JButton b1;
	JTextField tf1;
	JTextField tf2;
	JTextField tf3;
	JTextField tf4;

please assume the rest of codes are working well!

public void actionPerformed(ActionEvent ae) 
    {
    	String strSource = ae .getActionCommand( );
    	if(strSource=="Register")
    	{
    	Reg[0]=tf1.getText();
    	Reg[1]=tf2.getText();
    	Reg[2]=tf3.getText();
    	Reg[3]=tf4.getText();
    	        	
    	PrintWriter print;
    		try 
    		{
    	 	 print=new PrintWriter(new BufferedWriter(new FileWriter("test.txt")));
    		
    			for(int x=0; x<4; x++)
    			{
    	          		print.print(Reg[x] + " ");
    	        	}
    	        	print.close();
    		} 
    		catch (IOException e1) 
    		{ 
    			e1.printStackTrace();
    		}
    	}//end if
    }
2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

Do you mean the file is empty, or is there no file at all?
After line 9 try
System.out.println(Arrays.toString(Reg));
to help locate the problem.

0

hi
it does not change anyting :(
i put this line System.out.println(Arrays.toString(Reg));
on the line you specified.
Something is definetely missing but no idea...

0

If that didn't output anything then it suggests that your actionPerformed method isn't being called OR the if test on line 4 is failing...
NB You shouldn't compare Strings with ==
You must use the equals(...) method of the String class

Edited by JamesCherrill: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.