User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,047 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,270 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 623 | Replies: 1
Reply
Join Date: Jun 2008
Posts: 3
Reputation: Bouras is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Bouras Bouras is offline Offline
Newbie Poster

Java Random Access File... Help

  #1  
Jun 2nd, 2008
Hey... I present myself.. Youssef, 17 years old and i'm currently on my second year of the IB Program ... Computer IB HL i took. As some of you might know our IA = Internal Assessement is a Java Database and now mine is due wednesday... [PART I] and i need your guys help... I have a skeleton... you create a random acces file, and open it in a notepad.. then u run my program.. Team (only team needs to work for now) you can ADD a team ...click next next.. then you can click View team... (yet there there is a problem it only displays the first record tehn displays the message "No More Records" knowing that i have inputed more records iny my Add.. I would be eternally grateful if some one could help me by tonight plzzzzzzzzzz ..... so i could show my teacher tomorow.. If someone could fix me the View (READ RANDOM FILE) error.... and then have me a script thats working for EDIT & DELETE... cuz they're not in the book and i have no clue how to open then delete my record ... Delete is due tomorow, and the rest wednesday... i know all this for tonight is alot but some of few are experts and could do this in a 20-30 min ... so if some one could save me by correcting my VIEW, and writing me a script for EDIT/DELETE that would really help... if you want just give me an example of delete.. and ill try adapting it with my variables... anw... plz guyz help would be nice... Here are my codes..

  1. //BASKETBALL SKELETON//
  2. package Basketball;
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. public class Basketball extends JFrame
  9. {
  10.  
  11. private final Color colorValues[] =
  12. { Color.black, Color.blue, Color.red, Color.green };
  13. private JRadioButtonMenuItem colorItems[], fonts[];
  14. private JCheckBoxMenuItem styleItems[];
  15. private JLabel displayLabel;
  16. private ButtonGroup fontGroup, colorGroup;
  17. private int style;
  18.  
  19. // set up GUI
  20. public Basketball()
  21. {
  22. super( "BBGS SOFTWARE" );
  23.  
  24.  
  25.  
  26. // set up team menu and its menu items
  27. JMenu teamMenu = new JMenu( "Team" );
  28.  
  29.  
  30. // set up Add team menu item
  31. JMenuItem addTeam = new JMenuItem( "Add" );
  32. teamMenu.add( addTeam );
  33. addTeam.addActionListener(
  34.  
  35. new ActionListener() { // anonymous inner class
  36.  
  37.  
  38.  
  39. public void actionPerformed( ActionEvent event )
  40.  
  41. {
  42. new WriteRF();
  43.  
  44. }
  45.  
  46. }
  47.  
  48. );
  49.  
  50.  
  51.  
  52. JMenuItem editTeam = new JMenuItem( "Edit Team" );
  53. teamMenu.add( editTeam );
  54. editTeam.addActionListener(
  55.  
  56. new ActionListener() { // anonymous inner class
  57.  
  58. // display message dialog when user selects About...
  59. public void actionPerformed( ActionEvent event )
  60. {
  61.  
  62. }
  63.  
  64. } // end anonymous inner class
  65.  
  66. ); // end call to addActionListener
  67. JMenuItem deleteTeam = new JMenuItem( "Delete Team" );
  68. teamMenu.add( deleteTeam );
  69. deleteTeam.addActionListener(
  70.  
  71. new ActionListener() { // anonymous inner class
  72.  
  73. // display message dialog when user selects About...
  74. public void actionPerformed( ActionEvent event )
  75. {
  76.  
  77. }
  78.  
  79. } // end anonymous inner class
  80.  
  81. ); // end call to addActionListener
  82.  
  83.  
  84. // set up List team menu item
  85. JMenuItem listTeam = new JMenuItem( "View Teams" );
  86. teamMenu.add( listTeam );
  87. listTeam.addActionListener(
  88.  
  89. new ActionListener() { // anonymous inner class
  90.  
  91. // display message dialog when user selects About...
  92. public void actionPerformed( ActionEvent event )
  93. {
  94. new ReadRF();
  95. }
  96.  
  97. } // end anonymous inner class
  98.  
  99. ); // end call to addActionListener
  100.  
  101. // create menu bar and attach it to Basketball window
  102. JMenuBar bar = new JMenuBar();
  103. setJMenuBar( bar );
  104. bar.add( teamMenu );
  105.  
  106.  
  107.  
  108. // set up teamship menu and its menu items
  109. JMenu teamshipMenu = new JMenu( "Game" );
  110.  
  111.  
  112. // set up Add team menu item
  113. JMenuItem addGame = new JMenuItem( "Add Game" );
  114.  
  115. teamshipMenu.add( addGame );
  116. addGame.addActionListener(
  117.  
  118. new ActionListener() { // anonymous inner class
  119.  
  120. // display message dialog when user selects About...
  121. public void actionPerformed( ActionEvent event )
  122. {
  123. JOptionPane.showMessageDialog( Basketball.this,
  124. "Add a Game",
  125. "Adding", JOptionPane.PLAIN_MESSAGE );
  126. }
  127.  
  128. }
  129.  
  130. );
  131. JMenuItem editGame = new JMenuItem( "Edit/Delete Game" );
  132. teamshipMenu.add( editGame );
  133. editGame.addActionListener(
  134.  
  135. new ActionListener() { // anonymous inner class
  136.  
  137. // display message dialog when user selects About...
  138. public void actionPerformed( ActionEvent event )
  139. {
  140. JOptionPane.showMessageDialog( Basketball.this,
  141. "Edit a Game",
  142. "Editing", JOptionPane.PLAIN_MESSAGE );
  143. }
  144.  
  145. } // end anonymous inner class
  146.  
  147. ); // end call to addActionListener
  148.  
  149.  
  150. // set up List team menu item
  151. JMenuItem listGame = new JMenuItem( "List Games" );
  152. teamshipMenu.add( listGame );
  153. listGame.addActionListener(
  154.  
  155. new ActionListener() { // anonymous inner class
  156.  
  157. // display message dialog when user selects About...
  158. public void actionPerformed( ActionEvent event )
  159. {
  160. JOptionPane.showMessageDialog( Basketball.this,
  161. "Lisiting Games",
  162. "View", JOptionPane.PLAIN_MESSAGE );
  163. }
  164.  
  165. } // end anonymous inner class
  166.  
  167. ); // end call to addActionListener
  168.  
  169. // create menu bar and attach it to Basketball window
  170. bar.add( teamshipMenu );
  171.  
  172.  
  173.  
  174. // set up Exit menu item
  175. JMenu exitMenu = new JMenu( "Exit" );
  176.  
  177.  
  178. exitMenu.addActionListener(
  179.  
  180. new ActionListener() { // anonymous inner class
  181.  
  182. // terminate application when user clicks exitItem
  183. public void actionPerformed( ActionEvent event )
  184. {
  185. System.exit( 0 );
  186. }
  187.  
  188. } // end anonymous inner class
  189.  
  190. ); // end call to addActionListener
  191.  
  192. bar.add( exitMenu );
  193.  
  194.  
  195.  
  196. displayLabel = new JLabel( "Welcome", SwingConstants.CENTER );
  197. displayLabel.setForeground( colorValues[ 0 ] );
  198. displayLabel.setFont( new Font( "Serif", Font.PLAIN, 52 ) );
  199.  
  200. //getContentPane().setBackground( Color.CYAN );
  201. getContentPane().add( displayLabel, BorderLayout.PAGE_END );
  202.  
  203. setSize(800, 600 );
  204. setLocation(200, 240);
  205. setVisible( true );
  206.  
  207.  
  208.  
  209.  
  210. }
  211.  
  212. public static void main( String args[] )
  213. {
  214. Basketball application = new Basketball();
  215. application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  216. }
  217.  
  218.  
  219.  
  220.  
  221. }
  222.  
  223.  
  224. // end class Basketballs

  1. //WRITE RANDOM FILE
  2. package Basketball;
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.io.*;
  7. import javax.swing.*;
  8.  
  9.  
  10. public class WriteRF extends JFrame {
  11. private RandomAccessFile output;
  12. private GUI userInterface;
  13. private JButton enterButton, openButton;
  14.  
  15. private static final int NUMBER_RECORDS = 100;
  16.  
  17. // set up GUI
  18. public WriteRF()
  19. {
  20. super( "Write to random access file" );
  21.  
  22. // create instance of reusable user interface BankUI
  23. userInterface = new GUI( 2 ); // four textfields
  24. getContentPane().add( userInterface,
  25. BorderLayout.CENTER );
  26.  
  27.  
  28. openButton = userInterface.getDoTask1Button();
  29. openButton.setText( "Get File..." );
  30.  
  31. // register listener to call openFile when button pressed
  32. openButton.addActionListener(
  33.  
  34. // anonymous inner class to handle openButton event
  35. new ActionListener() {
  36.  
  37. // allow user to select file to open
  38. public void actionPerformed( ActionEvent event )
  39. {
  40. openFile();
  41. }
  42.  
  43. } // end anonymous inner class
  44.  
  45. ); // end call to addActionListener
  46.  
  47. // register window listener for window closing event
  48. addWindowListener(
  49.  
  50. // anonymous inner class to handle windowClosing event
  51. new WindowAdapter() {
  52.  
  53. // add record in GUI, then close file
  54. public void windowClosing( WindowEvent event )
  55. {
  56. if ( output != null )
  57. addRecord();
  58.  
  59. closeFile();
  60. }
  61.  
  62. } // end anonymous inner class
  63.  
  64. ); // end call to addWindowListener
  65.  
  66. // get reference to generic task button doTask2 in BankUI
  67. enterButton = userInterface.getDoTask2Button();
  68. enterButton.setText( "Next" );
  69. enterButton.setEnabled( false );
  70.  
  71. // register listener to call addRecord when button pressed
  72. enterButton.addActionListener(
  73.  
  74. // anonymous inner class to handle enterButton event
  75. new ActionListener() {
  76.  
  77. // add record to file
  78. public void actionPerformed( ActionEvent event )
  79. {
  80. addRecord();
  81. }
  82.  
  83. } // end anonymous inner class
  84.  
  85. ); // end call to addActionListener
  86.  
  87. setSize( 300, 150 );
  88. setVisible( true );
  89. }
  90.  
  91. // enable user to choose file to open
  92. private void openFile()
  93. {
  94. // display file dialog so user can select file
  95. JFileChooser fileChooser = new JFileChooser();
  96. fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
  97.  
  98. int result = fileChooser.showOpenDialog( this );
  99.  
  100. // if user clicked Cancel button on dialog, return
  101. if ( result == JFileChooser.CANCEL_OPTION )
  102. return;
  103.  
  104. // obtain selected file
  105. File fileName = fileChooser.getSelectedFile();
  106.  
  107. // display error if file name invalid
  108. if ( fileName == null || fileName.getName().equals( "" ) )
  109. JOptionPane.showMessageDialog( this, "Invalid File Name",
  110. "Invalid File Name", JOptionPane.ERROR_MESSAGE );
  111.  
  112. else {
  113.  
  114. // open file
  115. try {
  116. output = new RandomAccessFile( fileName, "rw" );
  117. enterButton.setEnabled( true );
  118. openButton.setEnabled( false );
  119. }
  120.  
  121. // process exception while opening file
  122. catch ( IOException ioException ) {
  123. JOptionPane.showMessageDialog( this, "File does not exist",
  124. "Invalid File Name", JOptionPane.ERROR_MESSAGE );
  125. }
  126.  
  127. } // end else
  128.  
  129. } // end method openFile
  130.  
  131. // close file and terminate application
  132. private void closeFile()
  133. {
  134. // close file and exit
  135. try {
  136. if ( output != null )
  137. output.close();
  138.  
  139. System.exit( 0 );
  140. }
  141.  
  142. // process exception while closing file
  143. catch( IOException ioException ) {
  144. JOptionPane.showMessageDialog( this, "Error closing file",
  145. "Error", JOptionPane.ERROR_MESSAGE );
  146.  
  147. System.exit( 1 );
  148. }
  149.  
  150. } // end method closeFile
  151.  
  152. // add one record to file
  153. private void addRecord()
  154. {
  155. int teamidnumber = 0;
  156. String fields[] = userInterface.getFieldValues();
  157. RaRecord record = new RaRecord();
  158.  
  159. // ensure account field has a value
  160. if ( ! fields[ GUI.TEAMID ].equals( "" ) ) {
  161.  
  162. // output values to file
  163. try {
  164. teamidnumber = Integer.parseInt( fields[ GUI.TEAMID ] );
  165.  
  166. if ( teamidnumber > 0 && teamidnumber <= NUMBER_RECORDS ) {
  167.  
  168. record.setTeamid( teamidnumber );
  169.  
  170. record.setTeam( fields[ GUI.TEAM ] );
  171.  
  172.  
  173. output.seek( ( teamidnumber - 1 ) *
  174. RaRecord.SIZE );
  175. record.write( output );
  176. }
  177. else
  178. {
  179. JOptionPane.showMessageDialog( this,
  180. "Team Id must be between 0 and 100",
  181. "ID number Invalid", JOptionPane.ERROR_MESSAGE );
  182. }
  183.  
  184. userInterface.clearFields(); // clear TextFields
  185.  
  186. } // end try
  187.  
  188. // process improper account number or balance format
  189. catch ( NumberFormatException formatException ) {
  190. JOptionPane.showMessageDialog( this,
  191. "Invalid ID",
  192. "Invalid Number Format", JOptionPane.ERROR_MESSAGE );
  193. }
  194.  
  195. // process exceptions while writing to file
  196. catch ( IOException ioException ) {
  197. JOptionPane.showMessageDialog( this,
  198. "Error writing to the file", "IO Exception",
  199. JOptionPane.ERROR_MESSAGE );
  200. closeFile();
  201. }
  202.  
  203. }
  204.  
  205. }
  206.  
  207.  
  208.  
  209. }
  210.  
  211. // END WRITE RANDOM FILE
  1. //READ RANDOM FILE//
  2. package Basketball;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.text.DecimalFormat;
  7. import javax.swing.*;
  8.  
  9.  
  10.  
  11. public class ReadRF extends JFrame {
  12. private GUI userInterface;
  13. private RandomAccessFile input;
  14. private JButton nextButton, openButton;
  15.  
  16.  
  17.  
  18. // set up GUI
  19. public ReadRF()
  20. {
  21. super( "View Team List" );
  22.  
  23. // create reusable user interface instance
  24. userInterface = new GUI( 2 );
  25. getContentPane().add( userInterface );
  26.  
  27. // configure generic doTask1 button from BankUI
  28. openButton = userInterface.getDoTask1Button();
  29. openButton.setText( "View Files for Listing..." );
  30.  
  31. // register listener to call openFile when button pressed
  32. openButton.addActionListener(
  33.  
  34. // anonymous inner class to handle openButton event
  35. new ActionListener() {
  36.  
  37. // enable user to select file to open
  38. public void actionPerformed( ActionEvent event )
  39. {
  40. openFile();
  41. }
  42.  
  43. } // end anonymous inner class
  44.  
  45. ); // end call to addActionListener
  46.  
  47. // configure generic doTask2 button from BankUI
  48. nextButton = userInterface.getDoTask2Button();
  49. nextButton.setText( "Next" );
  50. nextButton.setEnabled( false );
  51.  
  52. // register listener to call readRecord when button pressed
  53. nextButton.addActionListener(
  54.  
  55. // anonymous inner class to handle nextButton event
  56. new ActionListener() {
  57.  
  58. // read a record when user clicks nextButton
  59. public void actionPerformed( ActionEvent event )
  60. {
  61. readRecord();
  62. }
  63.  
  64. } // end anonymous inner class
  65.  
  66. ); // end call to addActionListener
  67.  
  68. // register listener for window closing event
  69. addWindowListener(
  70.  
  71. // anonymous inner class to handle windowClosing event
  72. new WindowAdapter() {
  73.  
  74. // close file and terminate application
  75. public void windowClosing( WindowEvent event )
  76. {
  77. closeFile();
  78. }
  79.  
  80. } // end anonymous inner class
  81.  
  82. ); // end call to addWindowListener
  83.  
  84. setSize( 300, 150 );
  85. setVisible( true );
  86.  
  87. } // end constructor
  88.  
  89. // enable user to select file to open
  90. private void openFile()
  91. {
  92. // display file dialog so user can select file
  93. JFileChooser fileChooser = new JFileChooser();
  94. fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
  95.  
  96. int result = fileChooser.showOpenDialog( this );
  97.  
  98. // if user clicked Cancel button on dialog, return
  99. if ( result == JFileChooser.CANCEL_OPTION )
  100. return;
  101.  
  102. // obtain selected file
  103. File fileName = fileChooser.getSelectedFile();
  104.  
  105. // display error is file name invalid
  106. if ( fileName == null || fileName.getName().equals( "" ) )
  107. JOptionPane.showMessageDialog( this, "Invalid File Name",
  108. "Invalid File Name", JOptionPane.ERROR_MESSAGE );
  109.  
  110. else {
  111.  
  112. // open file
  113. try {
  114. input = new RandomAccessFile( fileName, "r" );
  115. nextButton.setEnabled( true );
  116. openButton.setEnabled( false );
  117. }
  118.  
  119. // catch exception while opening file
  120. catch ( IOException ioException ) {
  121. JOptionPane.showMessageDialog( this, "File does not exist",
  122. "Invalid File Name", JOptionPane.ERROR_MESSAGE );
  123. }
  124.  
  125. } // end else
  126.  
  127. } // end method openFile
  128.  
  129. // read one record
  130. private void readRecord()
  131. {
  132. RaRecord record = new RaRecord();
  133.  
  134. // read a record and display
  135. try {
  136. do {
  137. record.read( input );
  138. } while ( record.getTeamid() == 0 );
  139.  
  140. String values[] = { String.valueOf( record.getTeamid() ),
  141. record.getTeam()};
  142. userInterface.setFieldValues( values );
  143. }
  144.  
  145. // close file when end-of-file reached
  146. catch ( EOFException eofException ) {
  147. JOptionPane.showMessageDialog( this, "No more records",
  148. "End-of-file reached", JOptionPane.INFORMATION_MESSAGE );
  149. closeFile();
  150. }
  151.  
  152. // process exceptions from problem with file
  153. catch ( IOException ioException ) {
  154. JOptionPane.showMessageDialog( this, "Error Reading File",
  155. "Error", JOptionPane.ERROR_MESSAGE );
  156.  
  157. System.exit( 1 );
  158. }
  159.  
  160. } // end method readRecord
  161.  
  162. // close file and terminate application
  163. private void closeFile()
  164. {
  165. // close file and exit
  166. try {
  167. if ( input != null )
  168. input.close();
  169.  
  170. System.exit( 0 );
  171. }
  172.  
  173. // process exception closing file
  174. catch( IOException ioException ) {
  175. JOptionPane.showMessageDialog( this, "Error closing file",
  176. "Error", JOptionPane.ERROR_MESSAGE );
  177.  
  178. System.exit( 1 );
  179. }
  180.  
  181. } // end method closeFile
  182.  
  183. }
  184. //END READ RANDOM FILE
  1. //GUI//
  2.  
  3. package Basketball;
  4.  
  5. import java.awt.*;
  6. import javax.swing.*;
  7.  
  8. public class GUI extends JPanel {
  9.  
  10. // label text for GUI
  11. protected final static String names[] = { "Team ID",
  12. "Team" };
  13.  
  14. // GUI components; protected for future subclass access
  15. protected JLabel labels[];
  16. protected JTextField fields[];
  17. protected JButton doTask1, doTask2;
  18. protected JPanel innerPanelCenter, innerPanelSouth;
  19.  
  20. protected int size; // number of text fields in GUI
  21.  
  22. // constants representing text fields in GUI
  23. public static final int TEAMID = 0, TEAM = 1;
  24.  
  25.  
  26. public GUI( int mySize )
  27. {
  28. size = mySize;
  29. labels = new JLabel[ size ];
  30. fields = new JTextField[ size ];
  31.  
  32. // create labels
  33. for ( int count = 0; count < labels.length; count++ )
  34. labels[ count ] = new JLabel( names[ count ] );
  35.  
  36. // create text fields
  37. for ( int count = 0; count < fields.length; count++ )
  38. fields[ count ] = new JTextField();
  39.  
  40. // create panel to lay out labels and fields
  41. innerPanelCenter = new JPanel();
  42. innerPanelCenter.setLayout( new GridLayout( size, 2 ) );
  43.  
  44. // attach labels and fields to innerPanelCenter
  45. for ( int count = 0; count < size; count++ ) {
  46. innerPanelCenter.add( labels[ count ] );
  47. innerPanelCenter.add( fields[ count ] );
  48. }
  49.  
  50. // create generic buttons; no labels or event handlers
  51. doTask1 = new JButton();
  52. doTask2 = new JButton();
  53.  
  54. // create panel to lay out buttons and attach buttons
  55. innerPanelSouth = new JPanel();
  56. innerPanelSouth.add( doTask1 );
  57. innerPanelSouth.add( doTask2 );
  58.  
  59. // set layout of this container and attach panels to it
  60. setLayout( new BorderLayout() );
  61. add( innerPanelCenter, BorderLayout.CENTER );
  62. add( innerPanelSouth, BorderLayout.SOUTH );
  63.  
  64. validate(); // validate layout
  65.  
  66. } // end constructor
  67.  
  68. // return reference to generic task button doTask1
  69. public JButton getDoTask1Button()
  70. {
  71. return doTask1;
  72. }
  73.  
  74. // return reference to generic task button doTask2
  75. public JButton getDoTask2Button()
  76. {
  77. return doTask2;
  78. }
  79.  
  80. // return reference to fields array of JTextFields
  81. public JTextField[] getFields()
  82. {
  83. return fields;
  84. }
  85.  
  86. // clear content of text fields
  87. public void clearFields()
  88. {
  89. for ( int count = 0; count < size; count++ )
  90. fields[ count ].setText( "" );
  91. }
  92.  
  93. // set text field values; throw IllegalArgumentException if
  94. // incorrect number of Strings in argument
  95. public void setFieldValues( String strings[] )
  96. throws IllegalArgumentException
  97. {
  98. if ( strings.length != size )
  99. throw new IllegalArgumentException( "There must be " +
  100. size + " Strings in the array" );
  101.  
  102. for ( int count = 0; count < size; count++ )
  103. fields[ count ].setText( strings[ count ] );
  104. }
  105.  
  106. // get array of Strings with current text field contents
  107. public String[] getFieldValues()
  108. {
  109. String values[] = new String[ size ];
  110.  
  111. for ( int count = 0; count < size; count++ )
  112. values[ count ] = fields[ count ].getText();
  113.  
  114. return values;
  115. }
  116.  
  117. }
  118. // END OF GUI