0

I am developing a simple GUI Calculator with the Java Swing library and bumped into the problem which I couldn't find a solution. I am just studying Java, so do not judge me strictly if the problem is simple.

The main idea of my problem is that I am trying to use Java.io "File handling" to save the number into the file and get that number when you re-open the calculator into the text field.

Here is the code:

public void actionPerformed (ActionEvent e) 
{
    //---------------------------------------------------------------
    if (e.getSource() == button_save) {
        save = mytext.getText();
        writeF(save); // problem is here "Unhandled exception type IOException"
    }
    else if (e.getSource() == button_recall) {
        recall = readF(); // also here
        mytext.setText(recall);
    }

User-defined method

public static void writeF(String memory)throws IOException{
    FileWriter writehandle = new FileWriter("C:\\Users\\Public\\Documents\\memory.txt");
    BufferedWriter bw = new BufferedWriter(writehandle);
    
    bw.write(memory);
    bw.close();
    writehandle.close();
}

public static String readF()throws IOException{
    FileReader readhandle = new FileReader("C:\\Users\\Public\\Documents\\memory.txt");
    BufferedReader br = new BufferedReader(readhandle);
    
    String num = br.readLine();
    
    br.close();
    readhandle.close();
    
    return num;
}

The full code you can find here

Arukhan
  • 1
  • 3

1 Answers1

0

Solution:

    else if (e.getSource() == button_save) {
        save = mytext.getText();
        try
        {
            writeF(save);
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        } // problem is here "Unhandled exception type IOException"
    }
    else if (e.getSource() == button_recall) {
        try 
        {
            recall = readF();
        } 
        catch (IOException e1) 
        {
            e1.printStackTrace();
        } // also here
        mytext.setText(recall);
    }
Arukhan
  • 1
  • 3