Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I can't key in the data

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I can't key in the data

    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??


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: I can't key in the data

    could you post the rest of your code? It won't compile without the other classes.

Similar Threads

  1. Virutal File Directory from Given dAta
    By hugsheals in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: July 28th, 2009, 04:39 AM
  2. Problem on sending vectors from Java server to C# client
    By MS_Dark in forum Java Networking
    Replies: 2
    Last Post: July 7th, 2009, 03:35 PM