Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
  1. #21
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    update, still looking for help. here is what the current code listed above does....

    as is, it freezes when you click the reserve button, no error, just disables both the jspinner, and the button.

    if i take out the while statement and the nested if statements, it writes to the file, but only saves the most recent date and name, not all of the names and dates entered.

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

  3. #23
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    not sure how to add line numbers to the [c ode] this file, and an empty .txt file you can create called reservations.txt with both in the same folder. the only code changed from the book is in the "private void reserveCarJButtonActionPerformed( ActionEvent event )" so we know everything else is fine.


    Java Code:
    // Exercise 25.15: CarReservation.java
    // 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;
       
       private BufferedReader input;
       private PrintWriter output;
    
       // 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 )
       {
    	   //create a new file object reserveFile for the file reservations.txt
          File reserveFile = new File ( "reservations.txt" );
          
          try 
          {
    	      //construct FileReader
    	      FileReader reserveFileReader = new FileReader ( reserveFile );
    	      
    	      //initialize BufferedReader input
    	      BufferedReader input = new BufferedReader ( reserveFileReader );
    	      
    	      // get selected date from JSpinner
    		  Date fullDate = ( Date ) dateJSpinner.getValue();
    		  String currentDate = fullDate.toString();
    		  
    		  //extract first 10 characters from currentDate
    		  String monthDay = currentDate.substring (0,10);
    		  
    		  //extract year from currentDate
    		  String year = currentDate.substring (24,28);
    		  
    		  //append monthDay and Year and store in the String currentDate
    		  currentDate = ( monthDay + " " + year );
    		  
    		  int dateCount = 1; // count the number of  reservations per day (max 4)
    		  String contents = input.readLine(); //read a line from the file
    		  
    		 // check if too many cars are reserved
    		  while ( contents != null )
      		  {
                // check reservation date
    			if ( contents.equals( currentDate ) )
    			{
    				// check reservation number
    				if ( dateCount < 4 )
    				{
    					//increment counter
    					dateCount++;
    				}
    				else
    				{
    					//inform user no more cars are left
    					JOptionPane.showMessageDialog ( this, "No more cars are available.",
    		   			"Out Of Cars", JOptionPane.ERROR_MESSAGE );
    		   			
    		   			// disable reserveCarJButton because no cars are left
    		   			reserveCarJButton.setEnabled ( false );
    		   			
    		   			//exit the method
    		   			
    				}// end inner if/else
    			 }// end outer if
    			 
    			 //read next line of the file
    			 input.readLine();
    			 
    			} // end while
    		  
    		  // close the file
    		  input.close();
    		  
    		  // open reservations.txt file for writing
    		  FileWriter reserveFileWriter = new FileWriter ( "reservations.txt" );
    		  output = new PrintWriter ( reserveFile );
    		  
    		  // write currentDate to file
    		  output.println ( currentDate );
    		  
    		  //write name to file
    		  output.println ( nameJTextField.getText() );
    		  
    		  // inform user that car has been reserved
    		  JOptionPane.showMessageDialog (this, "Your car has been reserved.");
    		  
    		  //close file
    		  output.close();
    		  
    	   } //end try
           catch ( IOException exception )
           {
    	      JOptionPane.showMessageDialog ( this, "IOException occurred." ); 
    	      dateJSpinner.setEnabled ( false );
    	      reserveCarJButton.setEnabled ( false );
    	   }
           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
    
    } // end class CarReservation

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

  5. #25
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    there is no data in it.

    well, you can start with none, there is 1 persons name in it like so:

    mon dec 05 2010
    James

    which is the date substring i setup, and the name i input in the textbox, from the time i ran it without the while statement in there.

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

    Default

    Hang on.....

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

    Default

    One problem I see is your while loop. If the condition that controls the while loop never changes, then the while loop will never end (with exceptions for break and similar constructs) and will loop forever. So you must ask yourself -- do you ever change the condition that controls the while loop? Do you ever change the value of the String contents within the while loop? If not, how can you fix this?

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

    Default

    Next

    • check out the FileWriter API for a constructor that will allow your code to append to an existing File rather than over-write it.
    • Initialize the FileWriter with the File you created earlier, not with a String.
    • Dont' forget to use that FileWriter to construct your PrintWriter


    Once these issues have been straightened out, you'll need to carefully study your if-logic and get it a little straightened out.

    Luck!

  9. #29
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    i was finally able to speak with my professor and cleared up the errors i was making.

    Fubarable you are right, one error is the lack of a return; statement, as well as a issue with the varible names for the filewriter.

    i REALLY want to say thank you for all the help you were, thanks to you i made it significantly further on this exercise and learned alot. i appreciate it.

    i look forward to continuing on this forum, and hopefully eventually helping others after im done GETTING help! :)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Saving a file in a gui
    By rav in forum AWT / Swing
    Replies: 9
    Last Post: 07-07-2010, 05:27 PM
  2. Saving Several arraylists into one file?
    By Cemi in forum New To Java
    Replies: 2
    Last Post: 04-26-2010, 02:04 PM
  3. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  4. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  5. Saving to a Text File
    By jadaleus in forum Advanced Java
    Replies: 2
    Last Post: 10-17-2008, 06:50 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
  •