• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

What is the error?

I got three files as follow :

// Fig. 17.4: BankUI.java
// A reusable GUI for the examples in this chapter.
package com.deitel.jhtp3.ch17;
import java.awt.*;
import javax.swing.*;

public class BankUI extends JPanel {
   protected final static String names[] = { "Account number",
      "First name", "Last name", "Balance",
      "Transaction Amount" };
   protected JLabel labels[];
   protected JTextField fields[];
   protected JButton doTask, doTask2;
   protected JPanel innerPanelCenter, innerPanelSouth;
   protected int size = 4;
   public static final int ACCOUNT = 0, FIRST = 1, LAST = 2,
                           BALANCE = 3, TRANSACTION = 4;                          

   public BankUI()
   {
      this( 4 );
   }

   public BankUI( int mySize )
   {
      size = mySize;
      labels = new JLabel[ size ];
      fields = new JTextField[ size ];

      for ( int i = 0; i < labels.length; i++ )
         labels[ i ] = new JLabel( names[ i ] );
           
      for ( int i = 0; i < fields.length; i++ )
         fields[ i ] = new JTextField();

      innerPanelCenter = new JPanel();
      innerPanelCenter.setLayout( new GridLayout( size, 2 ) );

      for ( int i = 0; i < size; i++ ) {
         innerPanelCenter.add( labels[ i ] );
         innerPanelCenter.add( fields[ i ] );
      }
     
      doTask = new JButton();
      doTask2 = new JButton();
      innerPanelSouth = new JPanel();      
      innerPanelSouth.add( doTask2 );
      innerPanelSouth.add( doTask );

      setLayout( new BorderLayout() );
      add( innerPanelCenter, BorderLayout.CENTER );
      add( innerPanelSouth, BorderLayout.SOUTH );
      validate();
   }

   public JButton getDoTask() { return doTask; }

   public JButton getDoTask2() { return doTask2; }

   public JTextField[] getFields() { return fields; }

   public void clearFields()
   {
      for ( int i = 0; i < size; i++ )
         fields[ i ].setText( "" );
   }

   public void setFieldValues( String s[] )
      throws IllegalArgumentException
   {
      if ( s.length != size )
         throw new IllegalArgumentException( "There must be "
            + size + " Strings in the array" );

      for ( int i = 0; i < size; i++ )
         fields[ i ].setText( s[ i ] );
   }

   public String[] getFieldValues()
   {
      String values[] = new String[ size ];

      for ( int i = 0; i < size; i++ )
         values[ i ] = fields[ i ].getText();

      return values;
   }
}

--------

// Fig. 17.4: BankAccountRecord.java
// A class that represents one record of information.
package com.deitel.jhtp3.ch17;
import java.io.Serializable;

public class BankAccountRecord implements Serializable {
   private int account;
   private String firstName;
   private String lastName;
   private double balance;
   
   public BankAccountRecord()
   {
      this( 0, "", "", 0.0 );
   }
 
   public BankAccountRecord( int acct, String first,
                             String last, double bal )
   {
      setAccount( acct );
      setFirstName( first );
      setLastName( last );
      setBalance( bal );
   }
   
   public void setAccount( int acct )
   {
      account = acct;
   }

   public int getAccount() { return account; }
   
   public void setFirstName( String first )
   {
      firstName = first;
   }

   public String getFirstName() { return firstName; }
   
   public void setLastName( String last )
   {
      lastName = last;
   }

   public String getLastName() { return lastName; }
   
   public void setBalance( double bal )
   {
      balance = bal;
   }

   public double getBalance() { return balance; }
}

I have no problem to compile them but not for the third file :

// Fig. 17.10: Record.java
// Record class for the RandomAccessFile programs.
package com.deitel.jhtp3.ch17;
import java.io.*;
import com.deitel.jhtp3.ch17.*;

public class Record extends BankAccountRecord {
 
   public Record()
   {
      this( 0, "", "", 0.0 );
   }

   public Record( int acct, String first,
                  String last, double bal )
   {
      super( acct, first, last, bal );
   }

   // Read a record from the specified RandomAccessFile
   public void read( RandomAccessFile file ) throws IOException
   {
      setAccount( file.readInt() );  
      setFirstName( padName( file ) );
      setLastName( padName( file ) );
      setBalance( file.readDouble() );
   }

   private String padName( RandomAccessFile f )
      throws IOException
   {
      char name[] = new char[ 15 ], temp;

      for ( int i = 0; i < name.length; i++ ) {
         temp = f.readChar();
         name[ i ] = temp;
      }    
     
      return new String( name ).replace( '\0', ' ' );
   }

   // Write a record to the specified RandomAccessFile
   public void write( RandomAccessFile file ) throws IOException
   {
      file.writeInt( getAccount() );
      writeName( file, getFirstName() );
      writeName( file, getLastName() );
      file.writeDouble( getBalance() );
   }

   private void writeName( RandomAccessFile f, String name )
      throws IOException
   {
      StringBuffer buf = null;

      if ( name != null )
         buf = new StringBuffer( name );
      else
         buf = new StringBuffer( 15 );

      buf.setLength( 15 );
      f.writeChars( buf.toString() );
   }

   // NOTE: This method contains a hard coded value for the
   // size of a record of information.
   public static int size() { return 72; }
}


I got this error message :

C:\Test\Record.java:7: Superclass com.deitel.jhtp3.ch17.BankAccountRecord of class com.deitel.jhtp3.ch17.Record not found.
public class Record extends BankAccountRecord {

How can I fix it?

Thank you very much.



0
yllee
Asked:
yllee
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Laminamia063099Commented:
The compiler can't find the compiled AccountRecord class.

What you need to do is compile these classes with the -d option.  Then, if re-compiling one later, make sure that you include the directory where you compiled them to later.

Example:
javac -d c:\classes *.java

If compiling later:
javac -classpath c:\classes Record.java

Good luck!...If you have any trouble, post a comment and we'll figure it out :)

Laminamia :)
0
 
AlexJ030200Commented:
Just a comment:

> If compiling later:
> javac -classpath c:\classes Record.java

should be :

javac -classpath c:\classes -d c:\classes Record.java

0
 
Laminamia063099Commented:
Thanks AlexJ!  

Laminamia :)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
prainCommented:
Here is the correct Answer:

First of all WHY DO YOU NEED

package com.deitel.jhtp3.ch17;
or
import com.deitel.jhtp3.ch17.*;

lines in your classes?.

In all classes in your program, remove all lines with the word "deitel". Then compile. Everything will be fine.

You know I have used this Deitel & Deitel's Java books for the last 3 years. There are pretty good examples in those books. But at the same time, they are misleading novice programmers, because they refer to a specific package, made by them. Not wll examples use these. Only some of them. So in the example you are using, the package ch17 is not being used. Therfore follow my advise. You will be all right.

Good Luck.
- prain :-)


0
 
prainCommented:
I mean all package... or import lines...
with the word "deitel"... shoudl be removed...to be specific.
0
 
ylleeAuthor Commented:
Thank you for your answer. It solved my problem, but have have another problem.

Follow is a problem which make use of previous 3 classes.  When I compile it under same directory, javac -classpath c:\classes -d c:\classes TransactionProcessor.java, it gives me 98 error.

// Fig. 17.14: TransactionProcessor.java
// Transaction processing program using RandomAccessFiles.
// This program reads a random-access file sequentially,
// updates record already written to the file, creates new
// record to be placed in the file and deletes data
// already in the file.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;
import com.jhtp3.ch17.*;
import javax.swing.*;

public class TransactionProcessor extends JFrame  {  
   private JDesktopPane desktop;
   private JButton open, updateRecord, newRecord, deleteRecord;
   private JInternalFrame mainDialog;
   private UpdateDialog updateDialog;
   private NewDialog newDialog;
   private DeleteDialog deleteDialog;
   private RandomAccessFile file;  
   private Record record;  
   
   public TransactionProcessor()
   {
      super( "Transaction Processor" );

      desktop = new JDesktopPane();
     
      mainDialog = new JInternalFrame();
      updateRecord = new JButton( "Update Record" );
      updateRecord.setEnabled( false );
      updateRecord.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               mainDialog.setVisible( false );
               updateDialog.setVisible( true );              
            }
         }
      );
     
      deleteRecord = new JButton( "Delete Record" );
      deleteRecord.setEnabled( false );
      deleteRecord.addActionListener(
         new ActionListener() {            
            public void actionPerformed( ActionEvent e )
            {
               mainDialog.setVisible( false );
               deleteDialog.setVisible( true );              
            }
         }
      );

      newRecord = new JButton( "New Record" );
      newRecord.setEnabled( false );
      newRecord.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               mainDialog.setVisible( false );
               newDialog.setVisible( true );              
            }
         }
      );

      open = new JButton( "New/Open File" );
      open.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               open.setEnabled( false );
               openFile();
               ActionListener l = new ActionListener() {
                  public void actionPerformed( ActionEvent e )
                               
                  {
                     mainDialog.setVisible( true );              
                  }
               };      
               updateDialog = new UpdateDialog( file, l );
               desktop.add( updateDialog );
               updateRecord.setEnabled( true );
               
               deleteDialog = new DeleteDialog( file, l );  
               desktop.add ( deleteDialog );
               deleteRecord.setEnabled( true );
               
               newDialog = new NewDialog( file, l );
               desktop.add( newDialog );
               newRecord.setEnabled( true );
               
            }
         }
      );

      Container c = mainDialog.getContentPane();
      c.setLayout( new GridLayout( 2, 2 ) );
      c.add( updateRecord );
      c.add( newRecord );
      c.add( deleteRecord );
      c.add( open );

      setSize( 400, 250  );
      mainDialog.setSize( 300, 80 );
      desktop.add( mainDialog, BorderLayout.CENTER );      
      getContentPane().add( desktop );
      addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               if ( file != null )
                  closeFile();

               System.exit( 0 );
            }
         }
      );
      show();
   }

   private void openFile()
   {
      JFileChooser fileChooser = new JFileChooser();

      fileChooser.setFileSelectionMode(
         JFileChooser.FILES_ONLY );

      int result = fileChooser.showOpenDialog( this );

      // user clicked Cancel button on dialog
      if ( result == JFileChooser.CANCEL_OPTION )
         return;

      File fileName = fileChooser.getSelectedFile();

      if ( fileName == null ||
           fileName.getName().equals( "" ) )
         JOptionPane.showMessageDialog( this,
            "Invalid File Name",
            "Invalid File Name",
            JOptionPane.ERROR_MESSAGE );
      else {
         // Open the file
         try {
            file = new RandomAccessFile( fileName, "rw" );
            updateRecord.setEnabled( true );
            newRecord.setEnabled( true );
            deleteRecord.setEnabled( true );
            open.setEnabled( false );
         }
         catch ( IOException e ) {
            JOptionPane.showMessageDialog( this,
               "File does not exist",
               "Invalid File Name",
               JOptionPane.ERROR_MESSAGE );
         }      
      }
   }
 
   private void closeFile()
   {
      try {
         file.close();
         System.exit( 0 );
      }
      catch( IOException ex ) {
         JOptionPane.showMessageDialog( this,
            "Error closing file",
            "Error", JOptionPane.ERROR_MESSAGE );
         System.exit( 1 );
      }
   }
 
   public static void main( String args[] )
   {
      new TransactionProcessor();
   }
}

class UpdateDialog extends JInternalFrame {
   private RandomAccessFile file;  
   private BankUI userInterface;
   private JButton cancel, save;
   private JTextField account;

   public UpdateDialog( RandomAccessFile f, ActionListener l )
   {
      super( "Update Record" );

      file = f;
      userInterface = new BankUI( 5 );

      cancel = userInterface.getDoTask();
      cancel.setText( "Cancel" );
      cancel.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               setVisible( false );
               userInterface.clearFields();    
            }
         }
      );
      cancel.addActionListener( l );
 
      save = userInterface.getDoTask2();
      save.setText( "Save Changes" );
      save.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               addRecord( getRecord() );
               setVisible( false );  
               userInterface.clearFields();            
            }
         }      
      );
      save.addActionListener( l );

      JTextField transaction =
         userInterface.getFields()[ BankUI.TRANSACTION ];
      transaction.addActionListener(
           new ActionListener() {
              public void actionPerformed( ActionEvent e )
              {
                 try {
                    Record record = getRecord();
                    double change = Double.parseDouble(
                             userInterface.getFieldValues()
                             [ BankUI.TRANSACTION ] );
                    String[] values = {
                        String.valueOf( record.getAccount() ),
                        record.getFirstName(),
                        record.getLastName(),
                        String.valueOf( record.getBalance()
                                        + change ),
                        "Charge(+) or payment (-)" };

                     userInterface.setFieldValues( values );
                 }
                 catch ( NumberFormatException nfe ) {
                    JOptionPane.showMessageDialog( new JFrame(),
                       "Invalid Transaction",
                       "Invalid Number Format",
                       JOptionPane.ERROR_MESSAGE );
                 }
              }
           }
      );

      account = userInterface.getFields()[ BankUI.ACCOUNT ];
      account.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               Record record = getRecord();

               if ( record.getAccount() != 0 )  {
                  String values[] = {
                     String.valueOf( record.getAccount() ),
                     record.getFirstName(),
                     record.getLastName(),
                     String.valueOf( record.getBalance() ),
                     "Charge(+) or payment (-)" };
                     userInterface.setFieldValues( values );
                  }      
               }
            }
      );
      getContentPane().add( userInterface,
                            BorderLayout.CENTER );
      setSize( 300, 175 );
      setVisible( false );
   }

   private Record getRecord()
   {
      Record record = new Record();

      try {
         int accountNumber = Integer.parseInt(
                                     account.getText() );

         if ( accountNumber < 1 || accountNumber > 100 ) {
            JOptionPane.showMessageDialog( this,
                 "Account Does Not Exist",
                 "Error", JOptionPane.ERROR_MESSAGE );
            return( record );
         }

         file.seek( ( accountNumber - 1 ) * Record.size() );
         record.read( file );
     
         if ( record.getAccount() == 0 )
            JOptionPane.showMessageDialog( this,
               "Account Does Not Exist",
               "Error", JOptionPane.ERROR_MESSAGE );
      }
      catch ( NumberFormatException nfe ) {
         JOptionPane.showMessageDialog( this,
            "Invalid Account",
            "Invalid Number Format",
            JOptionPane.ERROR_MESSAGE );
      }
      catch ( IOException io ) {
         JOptionPane.showMessageDialog( this,
            "Error Reading File",
            "Error", JOptionPane.ERROR_MESSAGE );
      }

      return record;
   }

   public void addRecord( Record record )
   {
      try {
         int accountNumber = record.getAccount();

         file.seek( ( accountNumber - 1 ) * Record.size() );            
         String[] values = userInterface.getFieldValues();                          
         record.write( file );
      }
      catch ( IOException io ) {
         JOptionPane.showMessageDialog( this,
            "Error Writing To File",
            "Error", JOptionPane.ERROR_MESSAGE );
      }
      catch ( NumberFormatException nfe ) {
         JOptionPane.showMessageDialog( this,
         "Bad Balance",
         "Invalid Number Format",
         JOptionPane.ERROR_MESSAGE );
      }
   }
}

class NewDialog extends JInternalFrame  {
   private RandomAccessFile file;  
   private BankUI userInterface;
   private JButton cancel, save;
   private JTextField account;

   public NewDialog( RandomAccessFile f, ActionListener l )
   {
      super( "New Record" );

      file = f;
      userInterface = new BankUI();

      cancel = userInterface.getDoTask();
      cancel.setText( "Cancel" );
      cancel.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
              setVisible( false );
              userInterface.clearFields();
            }  
         }
      );
      cancel.addActionListener( l );

      account = userInterface.getFields()[ BankUI.ACCOUNT ];
      save = userInterface.getDoTask2();
      save.setText( "Save Changes" );
      save.addActionListener(
           new ActionListener() {
              public void actionPerformed( ActionEvent e )
              {
                 addRecord( getRecord() );
                 setVisible( false );
                 userInterface.clearFields();
              }  
           }
      );
      save.addActionListener( l );

      getContentPane().add( userInterface,
                            BorderLayout.CENTER );
      setSize( 300, 150 );
      setVisible( false );
   }

   private Record getRecord()
   {
      Record record = new Record();

      try {
         int accountNumber = Integer.parseInt(
                                     account.getText() );

         if ( accountNumber < 1 || accountNumber > 100 ) {
            JOptionPane.showMessageDialog( this,
               "Account Does Not Exist",
               "Error", JOptionPane.ERROR_MESSAGE );
            return record;
         }

         file.seek( ( accountNumber - 1 ) * Record.size() );
         record.read( file );
      }
      catch ( NumberFormatException nfe ) {
         JOptionPane.showMessageDialog( this,
            "Account Does Not Exist",
            "Invalid Number Format",
            JOptionPane.ERROR_MESSAGE );
      }
      catch ( IOException io ) {
         JOptionPane.showMessageDialog( this,
            "Error Reading File",
            "Error", JOptionPane.ERROR_MESSAGE );
       }

       return record;
   }

   public void addRecord( Record record )
   {
      int accountNumber = 0;
      String[] fields = userInterface.getFieldValues();
     
      if ( record.getAccount() != 0 ) {
         JOptionPane.showMessageDialog( this,
            "Record Already Exists",
            "Error", JOptionPane.ERROR_MESSAGE );
         return;
      }

      // output the values to the file
      try {
         accountNumber =
            Integer.parseInt( fields[ BankUI.ACCOUNT ] );
         record.setAccount( accountNumber  );
         record.setFirstName( fields[ BankUI.FIRST ] );
         record.setLastName( fields[ BankUI.LAST ] );
         record.setBalance( Double.parseDouble(
                            fields[ BankUI.BALANCE ] ) );
         file.seek( ( accountNumber - 1 ) * Record.size() );
         record.write( file );
      }
      catch ( NumberFormatException nfe ) {
         JOptionPane.showMessageDialog( this,
            "Invalid Balance",
            "Invalid Number Format",
            JOptionPane.ERROR_MESSAGE );
      }
      catch ( IOException io ) {
         JOptionPane.showMessageDialog( this,
            "Error Writing To File",
            "Error", JOptionPane.ERROR_MESSAGE );
      }
   }
}


class DeleteDialog extends JInternalFrame {
   private RandomAccessFile file;  // file for output
   private BankUI userInterface;
   private JButton cancel, delete;
   private JTextField account;

   public DeleteDialog( RandomAccessFile f, ActionListener l )
   {
      super( "Delete Record" );

      file = f;
      userInterface = new BankUI( 1 );

      cancel = userInterface.getDoTask();
      cancel.setText( "Cancel" );
      cancel.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               setVisible( false );  
            }  
         }
      );
      cancel.addActionListener( l );
 
      delete = userInterface.getDoTask2();
      delete.setText( "Delete Record" );
      delete.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
              addRecord( getRecord() );      
              setVisible( false );  
              userInterface.clearFields();  
            }  
         }      
      );
      delete.addActionListener( l );

      account = userInterface.getFields()[ BankUI.ACCOUNT ];
      account.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )  
            {
               Record record = getRecord();
            }  
         }
      );
      getContentPane().add( userInterface,
                            BorderLayout.CENTER );

      setSize( 300, 100 );
      setVisible( false );
   }

   private Record getRecord()
   {
      Record record = new Record();

      try {
         int accountNumber = Integer.parseInt(
                                     account.getText() );
         if ( accountNumber < 1 || accountNumber > 100 ) {
            JOptionPane.showMessageDialog( this,
               "Account Does Not Exist",
               "Error", JOptionPane.ERROR_MESSAGE );
            return( record );
         }

         file.seek( ( accountNumber - 1 ) * Record.size() );
         record.read( file );
     
         if ( record.getAccount() == 0 )
            JOptionPane.showMessageDialog( this,
               "Account Does Not Exist",
               "Error", JOptionPane.ERROR_MESSAGE );
      }
      catch ( NumberFormatException nfe ) {
         JOptionPane.showMessageDialog( this,
            "Account Does Not Exist",
            "Invalid Number Format",
            JOptionPane.ERROR_MESSAGE );
      }
      catch ( IOException io ) {
        JOptionPane.showMessageDialog( this,
           "Error Reading File",
           "Error", JOptionPane.ERROR_MESSAGE );
      }

      return record;
   }

   public void addRecord( Record record )
   {
      if ( record.getAccount() == 0 )
         return;

      try {

         int accountNumber = record.getAccount();

         file.seek( ( accountNumber - 1 ) * Record.size() );
         record.setAccount( 0 );
         record.write( file );
      }
      catch ( IOException io ) {
         JOptionPane.showMessageDialog( this,
            "Error Writing To File",
            "Error", JOptionPane.ERROR_MESSAGE );
      }
   }
}

It should be error free because I download it from www.deitel.com.

By the way, I do not set classpath in my autoexec.bat.  Would it be the problem.  And, in command line, javac -classpath c:\classes...., the c:\classes refer to directory I put all source files?

Thank you very much.
0
 
ylleeAuthor Commented:
Please ignore my last comment, I know what is the problem now.  In fact, package com.deitel...., is not require in each *.java.  I took it away and everything run smoothly.

Thank you very much.
0
 
Laminamia063099Commented:
Just a note.  By removing the package name, you are only finding a temporary solution to the problem.  This will happen again in the future if you do not learn to use packages.  

Any code that you use, except for your own, will come in a package.  You want the classpath to point to where the root directory of where the *.class files are found.

For instance:
If you saved the deittel class files (not the source files) to the following directory:

c:\classes\com\deittel\jhtp3\ch17

you'll need to include the directory C:\classes in your classpath.  Don't include that whole directory structure (c:\classes\...\ch17) because the compiler and the interpreter know how to find classes by their class name.

If you compile a class and you want to take advantage of the packages to make sure that your class named X isn't mistaken for a class named X that someone else wrote, compile it as such:

javac -d c:\classes X.java

This will ensure that your class is compiled and placed in the c:\classes directory.

If your package is com.yllee, then you class will be placed in the:
c:\classes\com\yllee

directory (by virtue of the class name).

Notice that javac creates a subdirectory inside the directory where you tell it to compile the classes to whenever a .java file includes a package name.  This provides the ability to have more than one class of the same name, but through the package name you have distinction.  You'll never have to include all of the subdirectories in your classpath, just the directory at the top (in this case, the c:\classes directory).

Good luck in all your programming.

Laminamia :)
0
 
ylleeAuthor Commented:
Adjusted points from 100 to 150
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now