here is my main program
import java.io.IOException; import java.util.NoSuchElementException; import java.util.Scanner; public class TransactionProcessor { private FileEditor dataFile; private RandomAccessMarkingRecord record; private MenuOption choices[] = { MenuOption.SEARCH, MenuOption.INSERT_MARKS,MenuOption.DELETE, MenuOption.END }; private Scanner input = new Scanner (System.in); //get the file name and open the file private boolean openFile() { try //attempt to open file { //call the helper method to open the file dataFile = new FileEditor( "students.dat"); }//end try catch(IOException ioException) { System.err.println( "Error opening file." ); return false; } //end catch return true; } //end method openFile //close file and terminate application private void closeFile() { try // close file { dataFile.closeFile(); }//end try catch ( IOException ioException ) { System.err.println("Error closing file."); System.exit(1); }//end catch }//end method closeFile //create, update or delete the record private void performAction(MenuOption action ) { int idNumber; // id number for record String firstName; // first name for record String lastName; // last name for record int examMark; // exam mark for record int assessment; // assessment mark for record try //attempt to manipulate files based on option selected { switch ( action ) //switch based on option selected { case SEARCH: System.out.println(); dataFile.readRecords(); break; case INSERT_MARKS: System.out.printf( "\n %s %s", "ENTER ID NUMBER, FIRST NAME, LAST NAME, EXAM MARK and ASSESSMENT MARK,\n", " ******(ID NUMBER MUST BE IN 4 DIGITS (BETWEEN 2000 AND 2099)****** \n"); idNumber = input.nextInt(); //read id number firstName = input.next(); //read first name lastName = input.next(); //read last name examMark = input.nextInt(); //read exam mark assessment = input.nextInt();//read assessment mark dataFile.newRecord(idNumber, firstName, lastName, examMark, assessment );// create new record break; case DELETE: System.out.print( "\n Enter an id number to delete : " ); idNumber = input.nextInt(); dataFile.deleteRecord ( idNumber );//delete record break; default: System.out.println("Invalid action." ); break; }//end switch }//end try catch ( NumberFormatException format ) { System.err.println( "Bad input." ); }//end catch catch (IllegalArgumentException badIdNumber ) { System.err.println(badIdNumber.getMessage() ); }//end catch catch (IOException ioException) { System.err.println("Error writing to the file."); }//end catch catch (NoSuchElementException elementException ) { System.err.println( "Invalid input. Please try again."); input.nextLine(); //discard input so uder can try again }//end catch }//end method performAction //enable user to input menu choice private MenuOption enterChoice() { int menuChoice = 1; //display available options System.out.printf("\n %s \n %s \n %s \n %s \n %s ", "Enter your choice", "1 - List marks", "2- Add marking records","3- Delete a marking record", "4- End \n?"); try { menuChoice = input.nextInt(); } catch ( NoSuchElementException elementException ) { System.err.println( "Invalid input."); System.exit(1); }//end catch return choices[ menuChoice - 1 ]; //return choice from user }//end enterChoice public void processRequests() { openFile(); //get user's request MenuOption choice = enterChoice(); while( choice != MenuOption.END ) { performAction( choice ); choice = enterChoice(); }//end while closeFile(); }//end method processRequests }//end class TransactionProcessor
The problem is, when I key in the data, the screen displayed "error writing to the file"... how can it be??