If anyone has a minute, I could use some help. Been working on this program all day. I know it's hard trying to understand someone's code and logic...but I'm hoping there is an easy solution here.

My program needs to be able to read and write files at the push of a button. However, when I moved my input and output methods to the class that implements ActionListener...I get an IOexcepection error.

Where do I put the "throws IOException" line? I know it usually just goes after the main heading or after each method heading... But what about when I'm using a listener?

For example..this won't work..

public class runProgram implements ActionListener throws IOException

I couldn't get all my code to go in here nicely...so I just pieced some out to help show what I'm trying to do. I did however attach it if anyone wants to look at the mess.

import java.io.File;
import static java.lang.System.out;
import java.util.*;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class smartMonsterWindow extends JFrame
{

	public static void main (String args[]) throws IOException	
	{
		JFrame window = new smartMonsterWindow();
		window.show();
		//smartMonsterWindow.setSize (200,300);

	}	
	
	public class runProgram implements ActionListener
	{
                         fileInputMethod ();  //Method to Input File   //Exceptions errors here
                         fileOutputMethod (cookieGrid,blocksArray, );  //Method to Output File 
                }
Attachments
import java.io.File;
import static java.lang.System.out;
import java.util.*;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class smartMonsterWindow extends JFrame
{
	public static int blocksArray [][] = new int [200] [2];
	public static int cookiesArray [][] = new int [200] [2];
	public static int monsterRow=0;
	public static int monsterCol=0;
	public static int direction=0;
	public static int numBlocks=0;
	public static int numCookies=0;
	public static int rowSize=0;
	public static int colSize=0;
	public static int cookiesAdded=0;
	public static int blocksAdded=0;
	private JButton run;

	
	public static void main (String args[]) throws IOException	
	{
		JFrame window = new smartMonsterWindow();
		window.show();
		//smartMonsterWindow.setSize (200,300);
	//	run();
	}	
	
	
	public smartMonsterWindow() //INSIDE WINDOW
	{
	
		//Make the Window
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		setTitle ("SmartMonster");
		setSize (200,300);
		setLocation (80,50);

		//Add the components
		Container contentPane = getContentPane();
		contentPane.setLayout(new GridLayout (2, 2, 3, 3));
			
		JButton run = new JButton( "RUN" );
        contentPane.add(run);
				
        runProgram start = new runProgram( );
  		run.addActionListener( start );
	}
	

	public class runProgram implements ActionListener	 	 ///CLEAR MAZE CODE
	{
		public void actionPerformed( ActionEvent e )
		{
			int counter = 0;  //Counter used to display final Move count

			fileInputMethod ();  //Method to Input File
	
			//Make the Grid
			Grid cookieGrid = new Grid (rowSize, colSize, monsterRow, monsterCol, direction);
			GraphicGrid GUIGrid = new GraphicGrid (cookieGrid);
			addObjects (blocksArray, cookiesArray, numBlocks-1, numCookies-1, cookieGrid);
			int beenThereLessArray [][] = new int [rowSize][colSize];
			findBlocks (cookieGrid, beenThereLessArray);  //Makes each block value 1000, so monster never turns on them.
	
			fileOutputMethod (cookieGrid,blocksArray, cookiesArray, numBlocks-1, numCookies-1);  //Method to Output File
	
			//Clear the Grid
			while (!cookieGrid.isGridCleared( ))
			{
				
				switch (cookieGrid.whatIsInFront())
				{
					case 'c':
						lookForFood(cookieGrid);
						break;			
					case ' ':
						if (lookForFood(cookieGrid)== 'z')
						{
							beenThereLess(cookieGrid, beenThereLessArray);			
						}
						break;
					case '#':
						blockMethod (cookieGrid, beenThereLessArray);
						break;
					case 'e':
						blockMethod (cookieGrid, beenThereLessArray);
						break;	
				} //End Switch
				cookieGrid.move (); beenThereLessArray [cookieGrid.currentRow][cookieGrid.currentCol]++;counter++;
			}
				out.println ( cookieGrid.toString() );  //Send the picture
				out.println ("And the Final Number is: "+counter);
		}

	}

Hi dude
put the try catch block in the actionPerformed event

like this

public class runProgram implements ActionListener{
                       try{
                         fileInputMethod ();  //Method to Input File   //Exceptions errors here
                         fileOutputMethod (cookieGrid,blocksArray, );  //Method to Output File 
                      catch(IOException e}{System.err.println(e);}
                }
}

If anyone has a minute, I could use some help. Been working on this program all day. I know it's hard trying to understand someone's code and logic...but I'm hoping there is an easy solution here.

My program needs to be able to read and write files at the push of a button. However, when I moved my input and output methods to the class that implements ActionListener...I get an IOexcepection error.

Where do I put the "throws IOException" line? I know it usually just goes after the main heading or after each method heading... But what about when I'm using a listener?

For example..this won't work..

public class runProgram implements ActionListener throws IOException

^
No this is not correct

I couldn't get all my code to go in here nicely...so I just pieced some out to help show what I'm trying to do. I did however attach it if anyone wants to look at the mess.

import java.io.File;
import static java.lang.System.out;
import java.util.*;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class smartMonsterWindow extends JFrame
{

	public static void main (String args[]) throws IOException	
	{
		JFrame window = new smartMonsterWindow();
		window.show();
		//smartMonsterWindow.setSize (200,300);

	}
This question has already been answered. Start a new discussion instead.