• No results found

Tutorial 25 –

N/A
N/A
Protected

Academic year: 2021

Share "Tutorial 25 –"

Copied!
87
0
0

Loading.... (view fulltext now)

Full text

(1)

Tutorial 25 – Ticket Information

Application

Introducing Sequential-Access Files

Outline

25.1 Test-Driving the Write Event and Ticket Information Applications

25.2 Data Hierarchy 25.3 Files and Streams

25.4 Creating the Write Event Application: Writing to a File 25.5 Creating the Ticket Information Application

25.6 Using the finally block 25.7 Wrap-Up

(2)

• In this tutorial, you will learn to:

– Create, read from, write to and update files.

– Understand a computer’s data hierarchy.

– Become familiar with sequential-access file processing.

– Use the

FileReader

with

BufferedReader

capabilities to read a line of text from sequential-access files.

– Use the

FileWriter

with

PrintWriter

capabilities to write text to sequential-access files.

– Use the

finally

clause with exception handling to ensure

that certain actions are always performed.

(3)

Information Applications

Application Requirements

To create an application that reads event information from a file, you will need first to create another application that writes event information to a file. The user should be provided with input fields for the day, time, price, name and description of each event. The user should be able to specify the file’s location and name.

(4)

Application Requirements

A local town has asked you to write an application that allows its residents to view community events for the current month, such as concerts, sporting events, movies and other forms of entertainment. The events have already been written to the file calendar.txt using the Write Event application. When the user selects a day, the application should indicate whether there are events scheduled for that day. The application should list the scheduled events and allow the user to select a particular event. The application should then display the time, price and a brief description of the event. The application should inform the user when there are no events scheduled for a selected day.

(5)

Information Applications (Cont.)

Figure 25.1 Ticket Information application’s GUI.

JSpinner

JComboBox

lists any events

JTextArea displays event details

(6)

• Run the Ticket Information application

Type java TicketInformation

JSpinner displays the day of the current month

• Getting event information

– Select day that has an event

JComboBox displays first event of day

– Time, price and description appear in Description:

JTextArea

(7)

Information Applications (Cont.)

Figure 25.2 Ticket Information application displaying event information.

Event information displayed

(8)

• Opening a file

Click the Load File… JButton – Open calender.txt

Load File… JButton is disabled – All other components enabled

(9)

Information Applications (Cont.)

Figure 25.3 The Write Event application enables user to store event data.

JTextArea displays event description

JTextField for price of event

JSpinner for

day of event JSpinner for

time of event

JTextField for name of event

Open File…

JButton used to open a file that will store the event data

(10)

Figure 25.4 Dialog enables users to specify a file that will store event data.

(11)

Information Applications (Cont.)

Figure 25.5 Adding an event to calendar.txt

(12)

Figure 25.6 Entering another event and closing the file.

(13)

Information Applications (Cont.)

Figure 25.7 Using the Pick an event: JComboBox to select from multiple events occurring on the same day.

(14)

• Data hierarchy

– From bits up to larger data structures

• Characters

– Digits, letters and special symbols

• Character set

– Set of characters used to write applications and represent

data items on a computer

(15)

Figure 25.8 Data hierarchy.

(16)

• Bits

– Smallest data item a computer can support – Short for binary digit

• Can hold one of two values

– 0 – 1

• Characters are represented as patterns of bits

• Bytes

– 8 bits

– Characters in Java are Unicode

• 2 bytes (16 bits)

(17)

• Fields

– Group of characters that conveys some meaning

• Record

– Group of related fields

• File

– Group of related records

• Record key

– Identifies record as being part of some entity

– Distinguishes record from all other records

– One field in each record

(18)

• Sequential file

– Records stored in order by record-key field

• Database

– Group or related files

– Database management system used to design and manage

databases

(19)

Figure 25.9 Java’s conceptual view of an n-byte file.

• Streams

– Object that contains of sequence of characters

– Java views files as a sequential stream of bytes

(20)

• The java.io package

– BufferedReader

class

• Used for text input from a file

• Can read lines of text

• Don’t know how to read from a file

– Combine FileReader class (used to open a file) and

BufferedReader class to read from a file – PrintWriter

class

• Used for text output to a file

• Don’t know how to write to a file

• Combine FileWriter class (used to open a file) and

(21)

Writing to a File

When the user clicks the Open File… JButton Display a JFileChooser dialog

Retrieve the file (from the JFileChooser) as selected by the user Open the selected file for writing

Disable the Open File… JButton Enable the Enter JButton

Enable the Close File JButton

Reset input fields (priceJTextField, eventJTextField and descriptionJTextArea) When the user clicks the Enter JButton

Add the day of the event and a newline character to the file Add the time of the event and a newline character to the file Add the price of the event and a newline character to the file Add the name of the event and a newline character to the file Add a description of the event and a newline character to the file

Reset input fields (priceJTextField, eventJTextField and descriptionJTextArea) When the user clicks the Close File JButton

Close the file

Disable Enter JButton

Enable Open File… JButton Disable Close File JButton

Reset input fields (priceJTextField, eventJTextField and descriptionJTextArea)

(22)

Label the application’s components dayJLabel, timeJLabel, priceJLabel, eventJLabel,

descriptionJLabel

Application is run

Display a JFileChooser dialog fileChooser (JFileChooser) User clicks Open File…

JButton Retrieve the file selected by the user fileChooser, selectedFile

(File)

Open the selected file to write to outputFile (FileWriter), output (PrintWriter)

Disable the Open File… JButton openFileJButton

Enable the Enter JButton enterJButton

Enable the Close File JButton closeFileJButton

Reset input fields priceJTextField, eventJTextField,

descriptionJTextArea Add the day of the event and a newline

character to the file dayJSpinner, output User clicksEnterJButton

Add the time of the event and a newline

character to the file timeJSpinner, output

Add the price of the event and a newline

character to the file priceJTextField, output

Figure 25.10 ACE table for the Ticket Information application. (Part 1 of 2)

(23)

Writing to a File (Cont.)

Add the name of the event and a

newline character to the file eventJTextField, output

Add a description of the event and

a newline character to the file descriptionJTextArea, output

Reset input fields priceJTextField,

eventJTextField,

descriptionJTextArea

Close the file output User clicksClose File

JButton Disable the Enter JButton enterJButton

Enable the Open File… JButton openFileJButton Disable the Close File JButton closeFileJButton

Reset input fields priceJTextField,

eventJTextField, descriptionJTextArea

Figure 25.10 ACE table for the Ticket Information application. (Part 2 of 2)

(24)

Figure 25.11 Importing the java.io package into the WriteEvent class.

Importing the

java.io package

Figure 25.12 Declaring a PrintWriter object.

Declaring a

PrintWriter

object

(25)

Writing to a File (Cont.)

Figure 25.13 Displaying the JFileChooser dialog and retrieving the result.

Displaying the

JFileChooser

dialog

(26)

– Allows user to select file from disk

• If file does not exist, a new file will be created

– setDialogTitle

– sets title bar string of

JFileChooser – showOpenDialog

– displays dialog for opening a file

– JFileChooser.CANCEL_OPTION

• int constant returned if user clicks Cancel in JFileChooser

– JFileChooser.APPROVE_OPTION

• int constant returned if user clicks OK in JFileChooser

– JFileChooser.ERROR_OPTION

• int constant returned if an error occurs

(27)

Writing to a File (Cont.)

Figure 25.14 Exiting the method if the user clicks the Cancel JButton.

If the user clicks Cancel

JButton, the method returns

(28)

Figure 25.15 Retrieving the file selected by the user.

Getting the selected file

• getSelectedFile method of JFileChooser

– Returns File object that represents the file, directory path and name of file chosen by user

(29)

Writing to a File (Cont.)

Figure 25.16 Retrieving the name of the file using the getName method.

Retrieving the name of the selected file

(30)

Figure 25.17 Validating the file name.

Checking for a missing file name

(31)

Writing to a File (Cont.)

Figure 25.18 Opening the file to which the information will be written.

Create a FileWriter

and pass it to

PrintWriter

constructor

(32)

Figure 25.19 Changing the state of the JButtons.

(33)

Writing to a File (Cont.)

Figure 25.20 Displaying a JOptionPane if an IOException is caught.

Catch any

IOException thrown from the try block

(34)

Figure 25.21 Calling method resetUserInput.

Clearing the user input components

(35)

Writing to a File (Cont.)

Figure 25.22 WriteEvent application with opening files capability.

(36)

Figure 25.23 Creating a file.

(37)

Writing to a File (Cont.)

Figure 25.24 PrintWriter writing to a file.

Writing data to the file

(38)

• PrintWriter class

– println

method

file.

(39)

Writing to a File (Cont.)

Figure 25.26 Closing the PrintWriter.

Closing the

PrintWriter

• PrintWriter class

– close

method

• Closes the stream that writes to the file

(40)

Figure 25.27 Write Event application executing.

(41)

Writing to a File (Cont.)

Figure 25.28 Open File for Write Event dialog displaying contents of the template Write Event application’s directory.

(42)

Figure 25.29 Adding the Arts and Crafts Fair event to test.txt.

(43)

Writing to a File (Cont.)

Figure 25.30 Sequential-access file generated by Write Event application.

Data written to file

(44)

8 import java.util.Date;

9 import javax.swing.*;

10 import javax.swing.event.*;

11

12 public class WriteEvent extends JFrame 13 {

14 // JLabel and JSpinner to display day of month 15 private JLabel dayJLabel;

16 private JSpinner dayJSpinner;

17

18 // JLabel, JSpinner and JSpinner.DateEditor to display time 19 private JLabel timeJLabel;

20 private JSpinner timeJSpinner;

21

22 // JLabel and JTextField to display price 23 private JLabel priceJLabel;

24 private JTextField priceJTextField;

25

Importing the package java.io

(45)

WriteEvent.java (2 of 13)

29

30 // JLabel and JTextArea to display event description 31 private JLabel descriptionJLabel;

32 private JTextArea descriptionJTextArea;

33

34 // JButtons to allow user to write to files 35 private JButton openFileJButton;

36 private JButton enterJButton;

37 private JButton closeFileJButton;

38

39 // PrintWriter to write to files 40 private PrintWriter output;

41

42 // no-argument constructor 43 public WriteEvent()

44 {

45 createUserInterface();

46 } 47

48 // create and position GUI components; register event handlers 49 private void createUserInterface()

50 {

Declaring a new

PrintWriter

object

(46)

58 dayJLabel = new JLabel();

59 dayJLabel.setBounds( 20, 16, 75, 23 );

60 dayJLabel.setText( "Day:" );

61 contentPane.add( dayJLabel );

62

63 // set up dayJSpinner

64 dayJSpinner = new JSpinner (

65 new SpinnerNumberModel( 1, 1, 31, 1);

66 dayJSpinner.setBounds( 108, 16, 150, 23 );

67 contentPane.add( dayJSpinner );

68

69 // set up timeJLabel

70 timeJLabel = new JLabel();

71 timeJLabel.setBounds( 20, 46, 75, 23 );

72 timeJLabel.setText( "Time:" );

73 contentPane.add( timeJLabel );

74

(47)

WriteEvent.java (4 of 13)

78 timeJSpinner.setEditor(

79 new JSpinner.DateEditor( timeJSpinner, "HH:mm" ) );

80 contentPane.add( timeJSpinner );

81

82 // set up priceJLabel

83 priceJLabel = new JLabel();

84 priceJLabel.setBounds( 20, 76, 75, 23 );

85 priceJLabel.setText( "Price:" );

86 contentPane.add( priceJLabel );

87

88 // set up priceJTextField

89 priceJTextField = new JTextField();

90 priceJTextField.setBounds( 108, 76, 150, 23 );

91 contentPane.add( priceJTextField );

92

93 // set up eventJLabel

94 eventJLabel = new JLabel();

95 eventJLabel.setBounds( 20, 106, 75, 23 );

96 eventJLabel.setText( "Event:" );

97 contentPane.add( eventJLabel );

98

(48)

106 descriptionJLabel.setBounds( 20, 136, 75, 23 );

107 descriptionJLabel.setText( "Description:" );

108 contentPane.add( descriptionJLabel );

109

110 // set up descriptionJTextArea

111 descriptionJTextArea = new JTextArea();

112 descriptionJTextArea.setBounds( 108, 136, 150, 125 );

113 descriptionJTextArea.setLineWrap( true );

114 descriptionJTextArea.setWrapStyleWord( true );

115 contentPane.add( descriptionJTextArea );

116

117 // set up openFileJButton

118 openFileJButton = new JButton();

119 openFileJButton.setBounds( 5, 275, 100, 40 );

120 openFileJButton.setText( "Open File..." );

121 contentPane.add( openFileJButton );

122 openFileJButton.addActionListener(

123

(49)

WriteEvent.java (6 of 13)

127 public void actionPerformed( ActionEvent event ) 128 {

129 openFileJButtonActionPerformed( event );

130 } 131

132 } // end anonymous inner class 133

134 ); // end call to addActionListener 135

136 // set up enterJButton

137 enterJButton = new JButton();

138 enterJButton.setBounds( 106, 275, 80, 40 );

139 enterJButton.setText( "Enter" );

140 enterJButton.setEnabled( false );

141 contentPane.add( enterJButton );

142 enterJButton.addActionListener(

143

144 new ActionListener() // anonymous inner class 145 {

146 // event handler called when enterJButton is clicked 147 public void actionPerformed( ActionEvent event ) 148 {

(50)

156 // set up closeFileJButton

157 closeFileJButton = new JButton();

158 closeFileJButton.setBounds( 186, 275, 95, 40 );

159 closeFileJButton.setText( "Close File" );

160 closeFileJButton.setEnabled( false );

161 contentPane.add( closeFileJButton );

162 closeFileJButton.addActionListener(

163

164 new ActionListener() // anonymous inner class 165 {

166 // event handler called when closeFileJButton is clicked 167 public void actionPerformed( ActionEvent event )

168 {

169 closeFileJButtonActionPerformed( event );

170 } 171

172 } // end anonymous inner class 173

(51)

WriteEvent.java (8 of 13)

177 setTitle( "WriteEvent" ); // set title bar string 178 setSize( 290, 345 ); // set window size

179 setVisible( true ); // display window 180

181 } // end method createUserInterface 182

183 // open a file for writing

184 private void openFileJButtonActionPerformed( ActionEvent event ) 185 {

186 // display file dialog so user can select file to open 187 JFileChooser fileChooser = new JFileChooser();

188 fileChooser.setDialogTitle( "Open File for Write Event" );

189 int result = fileChooser.showOpenDialog( this );

190

191 // if user clicked Cancel JButton on dialog, return 192 if ( result == JFileChooser.CANCEL_OPTION )

193 {

194 return; // exit method openFileJButtonActionPerformed 195 }

196

Displaying the

JFileChooser

dialog User clicks

Cancel JButton

Exit the method

(52)

204 if ( fileName.equals( "" ) )   205 {

206 JOptionPane.showMessageDialog( this, "File name missing.", 207 "File Name Missing", JOptionPane.ERROR_MESSAGE );

208 } 209 else 210 {

211 // open file 212 try

213 {

214 // append information to fileName 215 FileWriter outputFile =

216 new PrintWriter( selectedFile, true );

217 output = new BufferedWriter( outputFile );

218

219 // change state of JButtons 220 openFileJButton.setEnabled( false );

221 enterJButton.setEnabled( true );

Get the selected file Get the selected file name

Create a

FileWriter and pass it to the

PrintWriter

constructor Validate the file name

(53)

WriteEvent.java (10 of 13)

226 JOptionPane.showMessageDialog( this,

227 “Cannot open the file“ + fileName + “.”, "Error", 228 JOptionPane.ERROR_MESSAGE );

229 }   230

231 } // end else 232

233 // reset JButtons to initial states 234 resetUserInput();

235

236 } // end method openFileJButtonActionPerformed 237

238 // save data entered to specified file

239 private void enterJButtonActionPerformed( ActionEvent event ) 240 {

241 // write day to file

242 output.println( dayJSpinner.getvalue() );

243

244 // retrieve time entered by user 245 String time = String.valueOf( timeJSpinner.getValue() );

246 time = time.substring( 11, 16 );

247

Catch any

IOException

thrown from the

try block

Write data to the file

(54)

255 output.println( eventJTextField.getText() );

256

257 // write event description to file 258 output.println( descriptionJTextArea.getText() );

259

260 // clear JTextFields 261 resetUserInput();

262

263 } // end method enterJButtonActionPerformed 264

265 // file is closed after user is finished with it

266 private void closeFileJButtonActionPerformed( ActionEvent event ) 267 {

268 // close file 269 output.close();

270

Write data to the file

Close the file

(55)

WriteEvent.java (12 of 13)

274 closeFileJButton.setEnabled( false );

275

276 // clear JTextFields 277 resetUserInput();

278

279 } // end method closeFileJButtonActionPerformed 280

281 // clear JTextFields

282 private void resetUserInput() 283 {

284 priceJTextField.setText( "" );

285 eventJTextField.setText( "" );

286 descriptionJTextArea.setText( "" );

287

288 } // end method resetUserInput 289

(56)

297

298 } // end class WriteEvent

(57)

Application

When the user selects a date from the dateJSpinner Retrieve the day selected in dateJSpinner Open calendar.txt file to read from

While there are events left in the file

If the current event is for the day selected by the user Store in array daysEvents the event information

Increment eventNumber, which contains the number of events for the selected day Read the next event’s information

Close the file

If events are scheduled for that day

Add each event to eventJComboBox Else

Display "- No Events -" in the eventJComboBox

Display "No events today." in the descriptionJTextArea When the user selects an event from the eventJComboBox

Retrieve index of selected item in the eventJComboBox Display event information in the descriptionJTextArea

(58)

eventJLabel,

descriptionJLabel Retrieve the day selected in

dateJSpinner dateJSpinner User selects a date from

the dateJSpinner Open calendar.txt file to read from currentFile (FileReader),

input (BufferedReader) While there are events left in the file

If the current event is for the day selected by the user

Store in array daysEvents the event information

daysEvents,

input (BufferedReader)

Increment eventNumber, which contains the number of events for the selected day

eventNumber

Read the next event’s information input (BufferedReader)

Close the file input (BufferedReader)

Add each event to eventJComboBox daysEvents, eventJComboBox Display "- No Events -" in the

eventJComboBox eventJComboBox

Display "No events today." in the descriptionJTextArea

(59)

Application (Cont.)

Figure 25.33 Declaring an instance variable in the Ticket Information application.

Declare a

BufferedReader

object

(60)

Figure 25.34 Invoking the createEventList method.

Calling the

createEventList

method

(61)

Application (Cont.)

Figure 25.35 Calling the extractData method and clearing the eventJComboBox.

The extractData

method will read events from calendar.txt

(62)

• removeAllItems method of JComboBox

Figure 25.36 Displaying the events scheduled for the specified day.

Indicating that events are scheduled for the day Extracting the event name from the array and displaying it in the

eventJComboBox

Indicating that no events are scheduled for the day

(63)

Application (Cont.)

Figure 25.37 Declaring variables in the extractData method.

(64)

object

to read data from a sequential-access file.

Read a line from the file Create a FileReader

and pass it to the

BufferedReader

constructor

(65)

Application (Cont.)

• BufferedReader object

– readLine

method

• Reads characters from a stream, up to and including a newline

• Returns characters as a String

(66)

Figure 25.39 Reading through each line in a file.

Check to see if end of file has been reached

(67)

Application (Cont.)

Store event information in a row of the array

Figure 25.40 Sequentially reading event entries from the file.

(68)

Figure 25.41 Finding the next date in the file.

(69)

Application (Cont.)

Figure 25.42 Displaying an error message if there is an error reading from the file.

(70)

• finally block

– Typically contains resource-release code – Always executes

• Executes if no exception is thrown

• Executes if an exception is thrown

(71)

Figure 25.43 Adding a finally block to your code.

Code in the

finally block always executes

(72)

Close the input file

(73)

Figure 25.45 Ticket Information application with event names.

(74)

Figure 25.46 Displaying event information in descriptionJTextArea.

(75)

Figure 25.47 Completed Ticket Information application.

(76)

8 import javax.swing.*;

9 import javax.swing.event.*;

10 import java.io.*;

11

12 public class TicketInformation extends JFrame 13 {

14 // JLabel, JSpinner, and DateEditor to display date 15 private JLabel selectDateJLabel;

16 private JSpinner dateJSpinner;

17

18 // JLabel and JComboBox to display day's events 19 private JLabel pickEventJLabel;

20 private JComboBox eventJComboBox;

21

22 // JLabel and JTextArea to display details of events 23 private JLabel descriptionJLabel;

24 private JTextArea descriptionJTextArea;

25

(77)

Ticket-

Information.java (2 of 12)

29 // File selected by user 30 private File calendarFile;

31

32 // instance variables to store event information and number 33 private String[][] daysEvents = new String[ 10 ][ 5 ];

34 private int eventNumber;

35

36 // no-argument constructor 37 public TicketInformation() 38 {

39 createUserInterface();

40

41 createEventList(); // read file and display events on given day 42 }

43

44 // create and position GUI components; register event handlers 45 private void createUserInterface()

46 {

47 // get content pane for attaching GUI components 48 Container contentPane = getContentPane();

49

Declare a

BufferedReader

object

(78)

57 contentPane.add( dateJLabel );

58

59 // set up dateJSpinner

60 dateJSpinner = new JSpinner(

61 new SpinnerNumberModel( 1, 1, 31, 1 ) );

62 dateJSpinner.setBounds( 147, 16, 119, 23 );

63 contentPane.add( dateJSpinner );

64 dateJSpinner.addChangeListener(

65

66 new ChangeListener() // anonymous inner class 67 {

68 // event handler called when dateJSpinner is changed 69 public void stateChanged( ChangeEvent event )

70 {

71 dateJSpinnerStateChanged( event );

72 } 73

74 } // end anonymous inner class

(79)

Ticket-

Information.java (4 of 12)

78 // set up eventJLabel

79 eventJLabel = new JLabel();

80 eventJLabel.setBounds( 16, 67, 100, 23 );

81 eventJLabel.setText( "Pick an event: " );

82 contentPane.add( eventJLabel );

83

84 // set up eventJComboBox

85 eventJComboBox = new JComboBox();

86 eventJComboBox.setBounds( 16, 94, 250, 23 );

87 eventJComboBox.addItem( "- No Events -" );

88 contentPane.add( eventJComboBox );

89 eventJComboBox.addActionListener(

90

91 new ActionListener() // anonymous inner class 92 {

93 // event handler called when eventJComboBox is changed 94 public void actionPerformed( ActionEvent event )

95 {

96 eventJComboBoxActionPerformed( event );

97 } 98

99 } // end anonymous inner class

(80)

107 contentPane.add( descriptionJLabel );

108

109 // set up descriptionJTextArea

110 descriptionJTextArea = new JTextArea();

111 descriptionJTextArea.setBounds( 16, 168, 250, 125 );

112 descriptionJTextArea.setText( "No events today." );

113 descriptionJTextArea.setLineWrap( true );

114 descriptionJTextArea.setWrapStyleWord( true );

115 descriptionJTextArea.setEditable( false );

116 contentPane.add( descriptionJTextArea );

117

118 // set properties of application's window

119 setTitle( "Ticket Information" ); // set title bar string 120 setSize( 292, 340 ); // set window size

121 setVisible( true ); // display window 122

123 } // end method createUserInterface 124

(81)

Ticket-

Information.java (6 of 12)

128 createEventList();

129

130 } // end method dateJSpinnerStateChanged 131

132 // display event information

133 private void eventJComboBoxActionPerformed( ActionEvent event ) 134 {

135 int eventNumber = eventJComboBox.getSelectedIndex();

136 descriptionJTextArea.setText(

137 daysEvents[ eventNumber ][ 1 ] + "\n" + // time 138 daysEvents[ eventNumber ][ 2 ] + "\n" + // price 139 daysEvents[ eventNumber ][ 4 ] ); // description 140

141 } // end method eventJComboBoxActionPerformed 142

143 // read events for current day and display events in application 144 private void createEventList()

145 {

146 // get data from file 147 extractData();

148

Create a list of events when the

dateJSpinner is changed

Extract the data from the file

Display the event information in

description- JTextArea

(82)

156 // add events to the eventJComboBox 157 for ( int x = 0; x < eventNumber; x++ ) 158 { 159 eventJComboBox.addItem( daysEvents[ x ][ 3 ] );

160 } 161 } 162 else // no events for the day 163 { 164 eventJComboBox.addItem( "- No Events -" );

165 descriptionJTextArea.setText( "No events today." );

166 } 167

168 } // end method createEventList 169

170 // read data from file 171 private void extractData() 172 {

173 eventNumber = 0;

Add the events for the day to

eventJComboBox

(83)

Ticket-

Information.java (8 of 12)

177 String currentDate = String.valueOf( date );

178

179 // initialize daysEvents array 180 initialize();

181

182 // find and display events for current date 183 try 184 { 185 // get file 186 calendarFile = new File( "calendar.txt" );

187

188 // open file 189 FileReader currentFile = new FileReader( calendarFile );

190 input = new BufferedReader( currentFile );

191

192 // read a line from the file 193 String contents = input.readLine();

194

195 // while more lines are in the file 196 while ( contents != null ) 197 {

Get the date from the dateJSpinner

Create a

FileReader and pass it to

BufferedReader

constructor

(84)

205 daysEvents[ eventNumber ][ 3 ] = input.readLine();

206 daysEvents[ eventNumber ][ 4 ] = input.readLine();

207 eventNumber++;

208 } 209 else // if date was not equal 210 { 211 // move to next date in file 212 for ( int x = 0; x < 4; x++ ) 213 { 214 input.readLine();

215 } 216 } 217

218 // read a line from the file 219 contents = input.readLine();

220

221 } // end while 222 }

Store the event information in a two-dimensional array

Search the entire file

(85)

Ticket-

Information.java (10 of 12)

226 "Please make sure the file exists and is of the " + 227 "right format.", "I/O Error", 228 JOptionPane.ERROR_MESSAGE );

229

230 // disable components 231 eventJComboBox.setEnabled( false );

232 dateJSpinner.setEnabled( false );

233 } 234 finally // close the file 235 { 236 // close the file 237 try 238 { 239 input.close();

240 } 241 catch( IOException exception ) 242 { 243 JOptionPane.showMessageDialog( this, 244 "Please make sure the file exists and is of the " + 245 "right format.", "I/O Error", 246 JOptionPane.ERROR_MESSAGE );

247 } 248 }

Closing the file

(86)

256 for ( int i = 0; i <= 9; i++ ) 257 {

258 // for each of five fields per event 259 for ( int j = 0; j <= 4; j++ )

260 {

261 daysEvents[ i ][ j ] = "";

262 } 263 } 264

265 daysEvents[ 0 ][ 3 ] = "- No Events -";

266 daysEvents[ 0 ][ 4 ] = "No events today.";

267

268 } // end method initialize 269

(87)

Ticket-

Information.java (12 of 12)

273 TicketInformation application = new TicketInformation();

274 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

275

276 } // end method main 277

278 } // end class TicketInformation

References

Related documents

Mangosteen leaf extract increases melanogenesis in B16F1 melanoma cells by stimulating tyrosinase activity in vitro and by up-regulating tyrosinase gene expression.. Harborne,

The approach taken was to design, build and test two different versions of our basic approach: a genetic program which digs out relevant trading signal information from tick

each position’s closing price. A fund need not trade out of each individual position pro rata to fund a flow.. The trader here will execute most of the trade in the closing

Audience flow through the museum is, at an overall level, circular. Most visitors enter through the main entrance of the existing building, receive practical information about

At the end of the 3th loop iteration (i=2), the user changes the front panel control Numeric to value 4.?. Q22: What is the behavior when the

Results were obtained through a comparative analysis of the Veterinary Clinic Collection and a number of subject collections. The Veterinary Clinic Collection is defined as the

In this paper we will demonstrate that, given an abstract ontology describing a knowledge domain in terms of the concepts and the relations holding among them, it is possible to

In April 2013 State Police Forensic Service Department under framework of EU program „Prevention of and Fight against Crime” launched implementation of project “Implementation