Search code examples
javauser-interfaceevent-handlingfileinputstreamfileoutputstream

input and output streams with a GUI in Java


I can't figure out why I keep getting this error. I need to design a program that uses a GUI with a button to write Data to a file and a button to read data and display it in a text box. I'm getting in error that is telling me I need an annotation name after the token on my readData and writeData methods. Here is my code so far please go easy on me I am new to this site.

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.io.*;

public class Exercise_17_5 extends Application{
    @Override 
    public void start(Stage primaryStage) throws FileNotFoundException, IOException{
        BorderPane pane = new BorderPane();

        HBox hbox = new HBox(10);
        hbox.setAlignment(Pos.CENTER);
        Button btWrite = new Button("Write");
        Button btRead = new Button("Read");
        hbox.getChildren().addAll(btWrite, btRead);
        hbox.setPadding(new Insets(5,5,5,5));
        pane.setBottom(hbox);

        TextArea taDisplay = new TextArea();
        taDisplay.setWrapText(true);
        pane.setCenter(taDisplay);


        primaryStage.setTitle("Exercise 17_05");
        Scene scene = new Scene(pane, 350,250);
        primaryStage.setScene(scene);
        primaryStage.show();

        btWrite.setOnAction(e -> writeData());
        btRead.setOnAction(e -> readData());

        private int[] array = {1,2,3,4,5};
        private double[] arrayDouble = {5.5};
        private Date[] dateArray = {new Date()};

        public void writeData() throws IOException{
            try(ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("Exercise15_05.dat"));
                    ){
                output.writeObject(array);
                output.writeObject(arrayDouble);
                output.writeObject(dateArray);
            }
        }

        public void readData() throws IOException{
            try(ObjectInputStream input = new ObjectInputStream(new FileInputStream("Exercise15_05.dat"));
                    ){
                int[] newInt = (int[])(input.readObject());
                double[] newDouble = (double[])(input.readObject());
                Date[] newDate = (Date[])(input.readObject());

                String existingText = taDisplay.getText();

                for(int i =0; i < newInt.length; i ++){
                    taDisplay.setText = (existingText +  " " + newInt[i]);
                }
                taDisplay.setText(existingText + "\n" + newDouble[0] + "\n" + newDate[0]);

            }
        }
    }

    public static void main(String[] args){
        Application.launch(args);
    }
}

Solution

  • I tried something else. I think you're right about me trying to access the TextArea the wrong way. Here is a code that worked for me

    public class Exercise_17_5 extends Application{
        @Override 
        public void start(Stage primaryStage){
            BorderPane pane = new BorderPane();
    
            HBox hbox = new HBox(10);
            hbox.setAlignment(Pos.CENTER);
            Button btWrite = new Button("Write");
            Button btRead = new Button("Read");
            hbox.getChildren().addAll(btWrite, btRead);
            hbox.setPadding(new Insets(5,5,5,5));
            pane.setBottom(hbox);
    
            TextArea taDisplay = new TextArea();
            taDisplay.setWrapText(true);
            pane.setCenter(taDisplay);
    
    
            primaryStage.setTitle("Exercise 17_05");
            Scene scene = new Scene(pane, 350,250);
            primaryStage.setScene(scene);
            primaryStage.show();
    
            btWrite.setOnAction(e -> {
                try {
                    writeData();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
    
            btRead.setOnAction(e -> {
                try {
                    taDisplay.setText(readData());
                } catch (Exception e1) {
                    taDisplay.setText("don't be stupid. You have to write data first");
                }
            });
    
        }
    
        int[] intArray = {1,2,3,4,5};
        double[] doubleArray = {5.5};
        Date[] dateArray = {new Date()};
    
        private String readData() throws Exception{
            ObjectInputStream input = new ObjectInputStream(new FileInputStream("Exercise17_05.dat"));
    
            int[] newIntArray = (int[])(input.readObject());
            double[] newDoubleArray = (double[])(input.readObject());
            Date[] newDateArray = (Date[])(input.readObject());
    
            String txt = "";
    
            for(int i = 0; i < newIntArray.length; i++){
                txt = txt + " " + newIntArray[i];
            }
    
            txt = txt + "\n" + newDoubleArray[0] + "\n" + newDateArray[0];
            input.close();
            return txt;
        }
    
        private void writeData() throws Exception {
    
            ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("Exercise17_05.dat"));
    
            output.writeObject(intArray);
            output.writeObject(doubleArray);
            output.writeObject(dateArray);
    
            output.close();
        }
    
    
    
        public static void main(String[] args){
            Application.launch(args);
        }
    }