What i need it to is write a GUI program that has one text field, one button, and one text area. The user can enter data into the text field, and then click the button. When the button is clicked, the program records the value in the text field to a file (one line) and then reads the data back from the file and displays it in the text area. Name the file your_last_name.txt. What i'm trying to right now is to output whatever is entered into the textfield to LastName.txt.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class lastName extends Frame {
        TextField textfield1;
        Button button;
        TextArea textarea = new TextArea ("",15,15,15);
        String temp;
          public LastName() {
            setSize(600, 400);
            setLayout(new FlowLayout());
            textfield1 = new TextField(8);
            button = new Button("Click Me");
            addWindowListener( new WindowAdapter()
              public void windowClosing(WindowEvent e)
            button.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                                try {
                                	    File myFile = new File("LastName.txt");          
                                        FileOutputStream outFileStream;
                                        outFileStream = new FileOutputStream(myFile);
                                        PrintWriter outStream = new PrintWriter(outFileStream);
                                        File myinFile = new File("LastName.txt");        
                                        FileReader myFileReader;
                                        myFileReader = new FileReader(myFile);
                                        BufferedReader myBufReader = new BufferedReader(myFileReader);
                                        String tempString;
                                        tempString = myBufReader.readLine();
                                } catch (FileNotFoundException e2) {
                                        // TODO Auto-generated catch block
                                } catch (IOException e1) {
                                        // TODO Auto-generated catch block
                                temp = textfield1.getText();
                        //      textarea.append(tempString);
                        //      textarea.append("\n");
        public static void main(String[] args) {
                LastName myGuiExampleObject = new LastName();

1) Where exactly lies your problem?
2) Why do write to the file again at line 58 when textfield1.getText() is unchanged?
3) The assignment is to display the string you got from reading the file, i.e. tempString, why print temp. On the outside it'll of course look the same, given your function to read works, but on the inside it's totally different.

This article has been dead for over six months. Start a new discussion instead.