0

below is the code i have and i am getting an unhandled IOException in the inputstreams:

import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
import static java.nio.file.StandardOpenOption.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class FinalProject extends JFrame implements ActionListener
{
    JLabel guess = new JLabel("Guess the picture: ");
    JLabel intro = new JLabel("welcome");
    Font bigFont = new Font("Veranda", Font.PLAIN, 20);
    JLabel out = new JLabel("");
    JTextField input = new JTextField("",10);
    JButton enter = new JButton("Enter");
    JButton play = new JButton("Play");
    Icon bb[];
    Icon sw[];
    JButton bbB[];
    JButton swB[];

    String[] bbArray = new String[8];
    String[] swArray = new String[8];
    Path a = Paths.get("C:\\Users\\Christine M\\Documents\\School\\INF-268-601\\Final Project\\BeautyAndTheBeastIn.txt");
    Path b = Paths.get("C:\\Users\\Christine M\\Documents\\School\\INF-268-601\\Final Project\\SnowWhiteIn.txt");
    InputStream bbFile = new BufferedInputStream(Files.newInputStream(a));
    BufferedReader readerBB = new BufferedReader(new InputStreamReader(bbFile));
    {
        for(int i = 0; i < bbArray.length; ++i)
        {
          try {
            bbArray[i] = readerBB.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }

    InputStream swFile = new BufferedInputStream(Files.newInputStream(b));
    BufferedReader readerSW = new BufferedReader(new InputStreamReader(swFile));
    {
        for(int i = 0; i < swArray.length; ++i)
        {
            try {
                swArray[i] = readerSW.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    JPanel panel = new JPanel(new BorderLayout());

    JMenuBar mainBar = new JMenuBar();
    JMenu menu1 = new JMenu("File");
    JMenu menu2 = new JMenu("Play");
    JMenuItem exit = new JMenuItem("Exit");
    JMenuItem item1 = new JMenuItem("Beauty and the Beast Quiz");
    JMenuItem item2 = new JMenuItem("Sleeping Beauty Quiz");

    int loopNumber;

    FinalProject()
    {
        super("Final Project");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        bb = new ImageIcon[8];
        for(int x = 0; x < bb.length; x++)
        {
            bb[x] = new ImageIcon("bb" + x + ".png");
        }
        sw = new ImageIcon[8];
        for(int x = 0; x < sw.length; x++)
        {
            sw[x] = new ImageIcon("sw" + x + ".png");
        }

        bbB = new JButton[8];
        for(int x = 0; x < bbB.length; x++)
        {
            bbB[x] = new JButton(bb[x]);
        }
        swB = new JButton[8];
        for(int x = 0; x < swB.length; x++)
        {
            swB[x] = new JButton(sw[x]);
        }

        add(intro);

        layoutComponents();
        addFont();
        composeMenu();
        addActionListeners();
        loopNumber = 0;
    }

    public void addFont()
    {

    }

    public void composeMenu()
    {
        setJMenuBar(mainBar);
        mainBar.add(menu1);
        mainBar.add(menu2);
        menu1.add(exit);
        menu2.add(item1);
        menu2.add(item2);
    }

    public void addActionListeners()
    {
        exit.addActionListener(this);
        item1.addActionListener(this);
        item2.addActionListener(this);
        enter.addActionListener(this);
        play.addActionListener(this);
        for(int x = 0; x < bbB.length; x++)
        {
            bbB[x].addActionListener(this);
        }
        for(int x = 0; x < swB.length; x++)
        {
            swB[x].addActionListener(this);
        }
    }

    public void layoutComponents()
    {
        panel.setLayout(new FlowLayout());
        add(panel);
    }

    public void actionPerformed(ActionEvent e)
    {
        //int loopNumber = 0;
        Object source = e.getSource();
            if(source == exit)
                System.exit(0);
            else if(source == item1)
            {
                for(int i = 0; i < bbB.length; i++)
                {
                    remove(bbB[i]);
                }
                for(int i = 0; i < bbB.length; i++)
                {
                    remove(swB[i]);
                }
                remove(intro);
                out.setText("");
                input.setText("");
                input.requestFocus();
                add(guess);
                add(bbB[0]);
                add(input);
                add(enter);
                add(out);
                input.requestFocus();
                repaint();
                revalidate();
            }
            else if(source == item2)
            {
                for(int i = 0; i <= bbB.length; i++)
                {
                    remove(bbB[i]);
                }
                for(int i = 0; i <= bbB.length; i++)
                {
                    remove(swB[i]);
                }
                remove(intro);
                out.setText("");
                input.setText("");
                input.requestFocus();
                add(guess);
                add(swB[0]);
                add(input);
                add(enter);
                add(out);
                input.requestFocus();
                repaint();
                revalidate();
            }

            else if(source == enter)
            {
                if(loopNumber <= bbArray.length);
                {
                    if(input.getText().equalsIgnoreCase(bbArray[loopNumber]))
                    {
                        loopNumber++;
                        remove(bbB[loopNumber - 1]);
                        remove(guess);
                        remove(input);
                        remove(enter);
                        remove(out);
                        input.setText("");
                        out.setText("Correct!");
                        add(guess);
                        if(loopNumber == 7)
                        {
                            if(input.getText().equalsIgnoreCase(bbArray[8]))
                            {
                                remove(bbB[8]);
                                remove(guess);
                                remove(input);
                                remove(enter);
                                remove(out);
                                input.setText("");
                                out.setText("YAY BITCH ALL Correct!");
                                add(guess);
                                add(bbB[loopNumber+1]);
                                add(input);
                                add(enter);
                                add(out);
                                input.requestFocus();
                                repaint();
                                revalidate();
                            }
                        }
                        add(bbB[loopNumber]);
                        add(input);
                        add(enter);
                        add(out);
                        input.requestFocus();
                        repaint();
                        revalidate();
                    }
                    else if(loopNumber <= swArray.length + 1)
                    {
                        if(input.getText().equalsIgnoreCase(swArray[loopNumber]))
                        {
                            remove(swB[loopNumber]);
                            remove(guess);
                            remove(input);
                            remove(enter);
                            remove(out);
                            input.setText("");
                            out.setText("Correct!");
                            add(guess);
                            add(swB[loopNumber+1]);
                            add(input);
                            add(enter);
                            add(out);
                            input.requestFocus();
                            repaint();
                            revalidate();
                            loopNumber++;
                        }
                        else
                        {
                            out.setText("Wrong, Try again!");
                            input.requestFocus();
                            add(out);
                        }
                      }
                }
            }
    }

    public static void main(String[] args) throws IOException
    {
        FinalProject k = new FinalProject();
        k.setVisible(true);
        k.setSize(450,350);
        //k.setResizable(false);    
    }

}

Edited by JamesCherrill: Moved to Programming forum

2
Contributors
2
Replies
18
Views
5 Months
Discussion Span
Last Post by JamesCherrill
0

You didn't quote the full error message, not even the relevant line number, so you are making this hard for anyone who wants to help.
But at a quick scan you appear to handle some IOExceptions correctly, so it's essential that you provide full error message details and, most important, the line number(s).

In the meantime you could check the API doc for newInputStream

public static InputStream newInputStream(Path path, OpenOption... options) throws IOException (etc etc)

... which means you need to handle that exception if you call that method.

Edited by JamesCherrill

0

On another topic re that code...

The use of a single shared actionPerformed for every control in the window leads to a horribly long if/ese/else if/... mess that's hard to read and harder to follow. Why not use this structure to break it up and make it much clearer with one method for each button, and a simple method reference to set the actionListener, eg...

public void exitAction(ActionEvent e) {
   // do whatever for an exit
}
public void enterAction(ActionEvent e) {
   // do whatever for an enter
}
... etc
...
exit.addActionListener(this::exitAction);
enter.addActionListener(this::enterAction);
... etc

Edited by JamesCherrill

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.