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");
                 
            add(textfield1);
                add(button);
           
            add(textarea);
                 
 
 
            addWindowListener( new WindowAdapter()
                 {
              public void windowClosing(WindowEvent e)
                        {
                System.exit(0);
              }
            }
                 );
 
                 
            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);
                                        outStream.println(textfield1.getText());
                                        
                                        File myinFile = new File("LastName.txt");        
                                        FileReader myFileReader;
                                       
                                        myFileReader = new FileReader(myFile);
                                        BufferedReader myBufReader = new BufferedReader(myFileReader);
                                        String tempString;
                                        tempString = myBufReader.readLine();
                                        outStream.println(textfield1.getText());
                                } catch (FileNotFoundException e2) {
                                        // TODO Auto-generated catch block
                                        e2.printStackTrace();
                                } catch (IOException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                }
                 
                                temp = textfield1.getText();
                               
                               
                              
                                textarea.append(temp);
                                textarea.append("\n");
                               
                        //      textarea.append(tempString);
                        //      textarea.append("\n");
                               
                               
                               
                               
 
              }
            });
          }
       
        public static void main(String[] args) {
               
                LastName myGuiExampleObject = new LastName();
            myGuiExampleObject.setVisible(true);
               
 
        }
 
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.