IOException in Java

3 weeks ago
New Contributor II

I'm working on a simple Chat Bot software that replies to what the user enters. I'm keeping a text file with the questions it answers and the answers to those questions. I'm reading the file with a scanner. This is also just my second application in which I employ a graphical user interface to interact with the user. I've got a TextField, a Label, and two buttons (One to enter the question asked and the other to exit the program). As far as I understand Java GUIs, you must extend JFrame in the main class. Another programme I made for a graphical software that estimates the area of a rectangle is as follows:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class RectangleProgram extends JFrame
    private static final int WIDTH = 400;
    private static final int HEIGHT = 200;

    private JLabel lengthL, widthL, areaL;
    private JTextField lengthTF, widthTF, areaTF;
    private JButton calculateB, exitB;

    //Button handlers:
    private CalculateButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    public RectangleProgram()
        lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
        widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
        areaL = new JLabel("Area: ", SwingConstants.RIGHT);

        lengthTF = new JTextField(10);
        widthTF = new JTextField(10);
        areaTF = new JTextField(10);

        //SPecify handlers for each button and add (register) ActionListeners to each button.
        calculateB = new JButton("Calculate");
        cbHandler = new CalculateButtonHandler();
        exitB = new JButton("Exit");
        ebHandler = new ExitButtonHandler();

        setTitle("Sample Title: Area of a Rectangle");
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(4, 3));

        //Add things to the pane in the order you want them to appear (left to right, top to bottom)

        setSize(WIDTH, HEIGHT);

    private class CalculateButtonHandler implements ActionListener
        public void actionPerformed(ActionEvent e)
            double width, length, area;
            length = Double.parseDouble(lengthTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.
            width = Double.parseDouble(widthTF.getText());
            area = length * width;
            areaTF.setText("" + area);

    public class ExitButtonHandler implements ActionListener
        public void actionPerformed(ActionEvent e)

    public static void main(String[] args)
        RectangleProgram rectObj = new RectangleProgram();

I'm using this to experiment with alternative GUIs. When I try to raise an IOException within the CalculateButtonHandler, it claims I can't. I want to use a very similar configuration for the Chat Bot software, however I'm wondering if there is a solution for the IOException, or if there is a better and easier approach to create a GUI?

0 Kudos
0 Replies