actionListener - IOException conflict

I am writing a program to simulate an ATM. I have a simple GUI interface in which the user clicks on buttons and enters text. The probliem is this: the program needs to read in user data from the GUI and then check it against a file.

I have written the code for reading/checking the info, and without the GUI it works fine. The problem is that I need a button click to intiate the process to check this data. Here is the code for the actionlistener for the button:

public void actionPerformed(ActionEvent e) throws IOException

{

if(evt.getSource() == quit)

{

System.exit(0);

}

if(evt.getSource() == etr)

{

//read data from acct & pw files

ReadAccountInfo myReader = new ReadAccountInfo();

myReader.readPIN();

myReader.readAcct();

ManageAcct myManager = new ManageAcct(myReader.getAcctData(),myReader.getPINData(),myReader.getBalanceData());

//get values from account & PIN fields

String acct = mainFrame.acctEntry.getText();

int account = Integer.parseInt(acct);

String pw = mainFrame.PINentry.getText();

int PIN = Integer.parseInt(pw);

if(myManager.validEntry(account,PIN))

{

setVisible(false);

OptionScreen optionFrame = new OptionScreen();

}

else

{

setVisible(false);

InvalidEntryScreen invalidFrame = new InvalidEntryScreen();

}

}

}

***the code for the Read Account Info is:

public void readPIN() throws IOException

{

File inFile = new File (PIN_FILE_NAME);

FileReader fReader = new FileReader (inFile);//read from filename

BufferedReader bReader = new BufferedReader (fReader);

int counter = 0;

try//may throw exception

{

String line = bReader.readLine(); //read 1st line of file

PIN = new int[DEFAULT_FILE_LENGTH];

account = new int[DEFAULT_FILE_LENGTH];

while (line != null)

{

String [] str_arr = line.split("\\s"); //split string in array

account [counter] = Integer.parseInt(str_arr[0]);

PIN [counter] = Integer.parseInt(str_arr[1]);

counter++;

line = bReader.readLine();

}

}

catch (Exception e)

{//handle exception

System.out.println (e.getMessage());

e.printStackTrace();

System.exit(0);

}

int [] pcopier = new int [counter]; int [] acopier = new int [counter];

for(int i=0;i<counter;i++) //eliminate empty entries

{

pcopier = PIN;

acopier = account;

}

PIN=pcopier;

account=acopier;

fReader.close();

}

My problem is that when I try and compile I get the error 'overridden method does not throw IOException' in regards to the actionPerformed method. I need the button click to intiate the reading of the file so I can use my checking method, so I am stuck as to a workaround..any advice?>

[2966 byte] By [michellema] at [2007-10-1 7:10:46]
# 1

Well I think you need to work on exception handling a little more!

First of all your declaring that readPin can throw an IOException, the only place where an IOException could occur is in the last line

fReader.close();

Now your thinking but the code in my try block can throw an IOException which is true but you have catch block that catches any type of exception. This is very very bad coding practice (catching all Exceptions totally defeates the idea of having Exceptions)! You should never want to do something like this ever. There could be a reason to do this but you would know when to do it.

Here is a better readPin():

(note I tried to make as little changes as possible)

public void readPIN() {

PIN = new int[DEFAULT_FILE_LENGTH];

account = new int[DEFAULT_FILE_LENGTH];

int counter = 0;

try // things that may throw exceptions

{

File inFile = new File (PIN_FILE_NAME); // can throw NullPointerException

FileReader fReader = new FileReader (inFile); // can throw FileNotFoundException

BufferedReader bReader = new BufferedReader (fReader);

String line = bReader.readLine(); // read 1st line of file can throw IOExcpetion

while (line != null) {

String [] str_arr = line.split("\\s"); // split string in array

account [counter] = Integer.parseInt(str_arr[0]);

PIN [counter] = Integer.parseInt(str_arr[1]);

counter++;

line = bReader.readLine(); // can throw IOExcpetion

}

} catch (FileNotFoundException fnfe) { // handle exception

System.out.println ("Error File not found - " + fnfe.getMessage());

} catch (IOException ioe) { // handle exception

System.out.println ("Error reading file - " + ioe.getMessage());

ioe.printStackTrace();

} finally {

try {

if (fReader != null)

fReader.close(); // can throw IOExcpetion

} catch (IOException ioe_finally) { // handle exception

System.out.println (ioe_finally.getMessage());

}

}

int [] pcopier = new int [counter]; int [] acopier = new int [counter];

for(int i=0;i<counter;i++) //eliminate empty entries

{

pcopier = PIN;

acopier = account;

}

PIN=pcopier;

account=acopier;

}

After you read through that you'll notice a much better use of exceptions and the code is better organized. Also notice readPin() no longer has to declare that it throws an IOException the reason for this is that all exceptions are handled in readPin(). Also notice the different exceptions that are caught and very importantly the finally block. It is very important to release resources that are held no matter what happens.

The only place where I might have exagerated a bit was in catching the FileNotFoundException(FNFE). Reason, FNFE is a subclass of IOException{IOE) so it would have been caught anyways. I inserted just for clarity.

Also notice how I didn't catch NullPointerException(NPE) why? Because NPE is a RuntimeException(RE). Know the differences between an RE and an Exception.

And lastly you should not have your actionPerformed method throw an IOE since readPin() will now not throw an IOE.

Hope this helps,

Earle Nietzel>

erna at 2007-7-9 18:16:23 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...