Results 1 to 5 of 5
  1. #1
    shelzmike is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default Sequential Access Problem - Nothing is being written to file.

    Okay, quick introduction. I am a student and am in an intro Java class. I am understanding most of it; however, I am having one issue with an exercise that I am working on.

    Here is the purpose:
    To create a simple car reservation application that stores the reservation information in a sequential access file.

    In the code what I need to happen is when the user clicks the Reserve Car button, the application needs to first check to see if there are cars already reserved for that day (there is a limit of 4 which is taken care of in the While statement). If the file content is null, it skips this while first.

    If the content is null, then I need to open the file for writing, which is taken care of using FileWriter. The program is then to write the date information using the currentDate variable created before the while statement. Next, it is supposed to write the name entered and display a message box stating that the car has been reserved (which it does display).

    However, I can add an infinate amount of reservations without getting the error message box stating that the max has been reached. So, I checked the reservations.txt file (which was intially created as a blank file) and I see that nothing at all is getting written to it. Here is the code, can anyone offer any insight into what I am missing. I am not getting any compliation or run-time errors, so I am sure this is just a simple problem I am overlooking. Thanks!

    Java Code:
    // This application allows users to input their names and
    // reserve cars on various days.
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.Date;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class CarReservation extends JFrame
    {
       // JLabel and JSpinner to display date
       private JLabel selectDateJLabel;
       private JSpinner dateJSpinner;
    
       // JLabel and JTextField to display name
       private JLabel nameJLabel;
       private JTextField nameJTextField;
    
       // JButton to reserve car
       private JButton reserveCarJButton;
    
       // Printwriter to write to files
       private PrintWriter output;
       
       //BufferedReader to read data from file
       private BufferedReader input;
       
       // file user will select
       private File reserveFile;
       
       // no-argument constructor
       public CarReservation()
       {
          createUserInterface();
       }
    
       // create and position GUI components
       public void createUserInterface()
       {
          // get content pane for attaching GUI components
          Container contentPane = getContentPane();
          
          // enable explicit positioning of GUI components
          contentPane.setLayout( null );
          
          // set up selectDateJLabel
          selectDateJLabel = new JLabel();
          selectDateJLabel.setBounds( 16, 16, 96, 23 );
          selectDateJLabel.setText( "Select the date:" );
          contentPane.add( selectDateJLabel );
    
          // set up dateJSpinner
          dateJSpinner = new JSpinner( new SpinnerDateModel() );
          dateJSpinner.setBounds( 16, 43, 250, 23 );
          dateJSpinner.setEditor( new JSpinner.DateEditor( 
             dateJSpinner, "MM/dd/yyyy" ) ); 
          contentPane.add( dateJSpinner );
          dateJSpinner.addChangeListener(
          
             new ChangeListener() // anonymous inner class
             {
                // event handler called when dateJSpinner is changed
                public void stateChanged( ChangeEvent event )
                {
                   dateJSpinnerChanged( event );
                }
                
             } // end anonymous inner class
             
          ); // end call to addActionListener            
                
          // set up nameJLabel
          nameJLabel = new JLabel();
          nameJLabel.setBounds( 16, 70, 100, 23 );
          nameJLabel.setText( "Name: " );
          contentPane.add( nameJLabel );
                
          // set up nameJTextField
          nameJTextField = new JTextField();
          nameJTextField.setBounds( 16, 97, 250, 23 );
          contentPane.add( nameJTextField );
          
          // set up reserveCarJButton
          reserveCarJButton = new JButton();
          reserveCarJButton.setBounds( 16, 130, 250, 23 );
          reserveCarJButton.setText( "Reserve Car" );
          contentPane.add( reserveCarJButton );
          reserveCarJButton.addActionListener(
    
             new ActionListener() // anonymous inner class
             {
                // event handler called when reserveCarJButton is clicked
                public void actionPerformed( ActionEvent event )
                {
                   reserveCarJButtonActionPerformed( event );
                }
    
             } // end anonymous inner class
    
          ); // end call to addActionListener
                
          // set properties of application's window
          setTitle( "Car Reservation" ); // set title bar string
          setSize( 287, 190 );           // set window size
          setVisible( true );            // display window
    
       } // end method createUserInterface
       
       // write reservation to a file
       private void reserveCarJButtonActionPerformed( ActionEvent event )
       {
       try
       {
           // get file
           reserveFile = new File( "reservations.txt" );
           
           // open file
           FileReader currentFile = new FileReader( reserveFile );
           input = new BufferedReader( currentFile );
           
           // get date from dateJSpinner and format
           Date fullDate = ( Date ) dateJSpinner.getValue();
           String currentDate = fullDate.toString();
           String monthDay = currentDate.substring( 0 , 10 );
           String year = currentDate.substring( 24, 27 );
           
           currentDate = ( monthDay + " " + year );
           
           // declare variable to store number of people who reserve a car
           int dateCount = 1;
           
           // read a line from the file and store
           String contents = input.readLine();
           
           // while loop to read file data
           while ( contents != null )
           {
               // if contents equal currentDate
               if ( contents.equals( currentDate ) )
               {
                   // check reservation number
                   if ( dateCount <4 )
                       dateCount++;
                   else
                   {
                       // display error message
                       JOptionPane.showMessageDialog( this, 
                          "There are no more cars available for this day!",
                          "All Cars Reserved", JOptionPane.ERROR_MESSAGE );
                   
                       // disable button
                       reserveCarJButton.setEnabled( false );
                       
                       // exit the method
                       return;
                   } // end else                 
               } // end if
               
             // read next line of file
             contents = input.readLine();
             
           } // end while
           
           // close the file
           input.close();
           
           FileWriter outputFile = new FileWriter( reserveFile, true );
           output = new PrintWriter( outputFile );
           
           // write day to file
           output.println( currentDate );
           
           // write reserved name to file
           output.println( nameJTextField.getText() );
           
           // display message that car has been reserved
           JOptionPane.showMessageDialog( this, "Your car has been reserved",
                   "Thank You", JOptionPane.INFORMATION_MESSAGE);
           
       } // end try
       catch ( IOException exception )
       {
           JOptionPane.showMessageDialog( this, "Please make sure the file exists " +
                   "and is of the right format.", "I/O Error",
                   JOptionPane.ERROR_MESSAGE );
           
           // disable buttons
           dateJSpinner.setEnabled( false );
           reserveCarJButton.setEnabled( false );
       } // end catch
       
       // clear nameJTextField
       nameJTextField.setText( "" );
           
       } // end method reserveCarJButtonActionPerformed
    
       // enable reserveCarJButton
       private void dateJSpinnerChanged( ChangeEvent event )
       {
          reserveCarJButton.setEnabled( true );
       
       } // end method dateJSpinnerChanged
       
       // main method
       public static void main( String[] args )
       {
          CarReservation application = new CarReservation();
          application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
       } // end method main

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    First off I would like to say this is VERY well written for an intro Java course. Where is your file located? I changed the file path to a file I created on my desktop directly and all I get still is "Please make sure the file exists"

  3. #3
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    Quote Originally Posted by xcallmejudasx View Post
    First off I would like to say this is VERY well written for an intro Java course. Where is your file located? I changed the file path to a file I created on my desktop directly and all I get still is "Please make sure the file exists"
    I agree this is some upper level stuff for an intro course. I think the problem might be were u typed the location of the file it's simply (filename.txt).
    Generally you have to put in the complete path(C:\........filename.txt).

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Quote Originally Posted by timkd127 View Post
    I agree this is some upper level stuff for an intro course. I think the problem might be were u typed the location of the file it's simply (filename.txt).
    Generally you have to put in the complete path(C:\........filename.txt).
    I don't know I think it's something we're missing. I declared reserverFile as new File("C\\Documents and Settings\\carsonrs\\Desktop\\reservations.txt") and still got invalid file error.

    I think it has something to do where the file is created. I think that should be placed maybe in the main or make it globally defined. doing reserveFile = new File() each time you access the reserveButton object seems redundant to me. It should only need to be created once. Or just throw a if(file !exists) in the beginning to create it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're forgetting to close the output file. This should be done in a finally block that you've added below the catch block. This will close the file and will flush it as well, and the flush will write the information to your text file.

Similar Threads

  1. Random File Access
    By viper110110 in forum New To Java
    Replies: 11
    Last Post: 11-28-2008, 01:28 AM
  2. remote file access problem
    By kib_tse in forum New To Java
    Replies: 2
    Last Post: 08-14-2008, 07:04 PM
  3. Text file over written
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-11-2007, 08:32 PM
  4. variable access from another file
    By riadmazloum in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:13 PM
  5. Problem with Sequential File and Arrays
    By rhivka in forum New To Java
    Replies: 8
    Last Post: 07-30-2007, 10:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •