Ghost in the code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rainman33
    New Member
    • Oct 2007
    • 5

    Ghost in the code

    Here's a weird one. I compile and run this code, and get Bob Jones as the account holder, and a Withdrawel <sic> JTextField where there should not be one. Anybody help me on this one?



    Code:
    // Exercise 4.16: AccountInformation.java
    // This application inputs and outputs account information.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class AccountInformation extends JFrame
    {
       // JPanel to group deposit components
       private JPanel enterJPanel;
    
       // JLabel and JTextField for deposits
       private JLabel depositJLabel;
       private JTextField depositJTextField;
    
       // JButton to initiate balance calculation
       private JButton enterJButton;
    
       // JPanel to group account information components
       private JPanel accountJPanel;
    
       // JLabel and JTextField for account holder's name
       private JLabel nameJLabel;
       private JTextField nameJTextField;
    
       // JLabel and JTextField for account number
       private JLabel accountNumberJLabel;
       private JTextField accountNumberJTextField;
    
       // JLabel and JTextField for balance
       private JLabel balanceJLabel;
       private JTextField balanceJTextField;
       
       // no-argument constructor
       public AccountInformation() 
       {
          createUserInterface();
       }
       
       // create and position GUI components; register event handlers
       private void createUserInterface()
       {
          // get content pane and set layout to null
          Container contentPane = getContentPane();
          contentPane.setLayout( null );
          
          // set up enterJPanel
          enterJPanel = new JPanel();
          enterJPanel.setLayout( null );
          enterJPanel.setBounds( 16, 16, 152, 126 );
          enterJPanel.setBorder( 
             new TitledBorder( "Enter information" ) );
          contentPane.add( enterJPanel );
          
          // set up depositJLabel
          depositJLabel = new JLabel();
          depositJLabel.setText( "Deposit amount:" );
          depositJLabel.setBounds( 16, 40, 140, 16 );
          enterJPanel.add( depositJLabel );
          
          // set up depositJTextField
          depositJTextField = new JTextField();
          depositJTextField.setText( "0" );
          depositJTextField.setBounds( 16, 56, 120, 21 );
          depositJTextField.setHorizontalAlignment( JTextField.RIGHT );
          enterJPanel.add( depositJTextField );
    
          // set up enterJButton
          enterJButton = new JButton();
          enterJButton.setText( "Enter" );
          enterJButton.setBounds( 16, 150, 152, 34 );
          contentPane.add( enterJButton );
          enterJButton.addActionListener(
    
             new ActionListener() // anonymous inner class
             {
                // event handler called when enterJButton is pressed
                public void actionPerformed( ActionEvent event )
                {
                   enterJButtonActionPerformed( event );
                }
    
             } // end anonymous inner class
    
          ); // end call to addActionListener
          
          // set up accountJPanel
          accountJPanel = new JPanel();
          accountJPanel.setLayout( null );
          accountJPanel.setBounds( 180, 16, 136, 170 );
          accountJPanel.setBorder( 
             new TitledBorder( "Account information" ) );
          contentPane.add( accountJPanel );
          
          // set up nameJLabel
          nameJLabel = new JLabel();
          nameJLabel.setText( "Name:" );
          nameJLabel.setBounds( 16, 24, 100, 16 );
          accountJPanel.add( nameJLabel );
    
          // set up nameJTextField
          nameJTextField = new JTextField();
          nameJTextField.setText( "Sue Purple" );
          nameJTextField.setBounds( 16, 40, 104, 21 );
          nameJTextField.setEditable( false );
          accountJPanel.add( nameJTextField );
    
          // set up accountNumberJLabel
          accountNumberJLabel = new JLabel();
          accountNumberJLabel.setText( "Account Number:" );
          accountNumberJLabel.setBounds( 16, 70, 140, 16 );
          accountJPanel.add( accountNumberJLabel );
    
          // set up accountNumberJTextField
          accountNumberJTextField = new JTextField();
          accountNumberJTextField.setText( "12345" );
          accountNumberJTextField.setBounds( 16, 86, 104, 21 );
          accountNumberJTextField.setEditable( false );
          accountNumberJTextField.setHorizontalAlignment( 
             JTextField.RIGHT );
          accountJPanel.add( accountNumberJTextField );
    
          // set up balanceJLabel
          balanceJLabel = new JLabel();
          balanceJLabel.setText( "Balance:" );
          balanceJLabel.setBounds( 16, 116, 100, 16 );
          accountJPanel.add( balanceJLabel );
          
          // set up balanceJTextField
          balanceJTextField = new JTextField();
          balanceJTextField.setText( "0" );
          balanceJTextField.setBounds( 16, 132, 104, 21 );
          balanceJTextField.setEditable( false );
          balanceJTextField.setHorizontalAlignment( JTextField.RIGHT );
          accountJPanel.add( balanceJTextField );
          
          // set properties of application's window
          setTitle( "Account Information" ); // set title bar text
          setSize( 325, 225 );               // set window size
          setVisible( true );                // display window
    
       } // end method createUserInterface
    
       // method called when enterJButton is pressed
       private void enterJButtonActionPerformed( ActionEvent event )
       {
          // display new balance
          balanceJTextField.setText( String.valueOf( 
             Integer.parseInt( depositJTextField.getText() ) - 
             Integer.parseInt( withdrawelJTextField.getText() ) ) );
    
          // clear depositJTextField
          depositJTextField.setText( "0" );    
    
       } // end method enterJButtonActionPerformed
    
       // main method
       public static void main( String[] args ) 
       {
          AccountInformation application = new AccountInformation();
          application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
       } // end method main
    
    } // end class AccountInformation


    I just can't figure this out. I sent in the code to my teacher, and he says he can't figure it out, either. How can the text, "Bob Jones", appear, when it does not exist anywhere in the code?

    Flummoxed.
    Last edited by rainman33; Oct 18 '07, 04:42 AM. Reason: Email notification
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What is suprising me now is that on 151 you have

    [CODE=java] ...Integer.pars eInt( withdrawelJText Field.getText() ) ) );[/CODE]
    and yet you have not defined a variable called withdrawelJText Field anywhere in your code.
    Are you sure this compiles?

    Comment

    • rainman33
      New Member
      • Oct 2007
      • 5

      #3
      I'm sorry. I guess it does not compile; it gives me that error. But it still runs the weird output. Could this be the reason? You can see on line 104 that the nameJTextField is set to "Sue Purple", and yet this text does not appear anywhere in the output. Instead, the text "Bob Jones" appears. Also, there is no code which sets up a withdrawelJLabe l, yet this label appears in the output. I guess the first thing to do is to go ahead and code a withdrawelJLabe l, as well as a withdrawelJText Field. But I'm still at a loss to figure out this screwy output. If I could figure out how to post an image in a reply, I would post the output. I guess I should have posted the outputted image in the first place.....

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by rainman33
        I'm sorry. I guess it does not compile; it gives me that error. But it still runs the weird output. Could this be the reason? You can see on line 104 that the nameJTextField is set to "Sue Purple", and yet this text does not appear anywhere in the output. Instead, the text "Bob Jones" appears. Also, there is no code which sets up a withdrawelJLabe l, yet this label appears in the output. I guess the first thing to do is to go ahead and code a withdrawelJLabe l, as well as a withdrawelJText Field. But I'm still at a loss to figure out this screwy output. If I could figure out how to post an image in a reply, I would post the output. I guess I should have posted the outputted image in the first place.....
        If the code does not compile, then a class file won't be created and you can't run the program. So which program is giving you the strange output?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          I guess the code that is running is not the code we're seeing here.

          kind regards,

          Jos

          Comment

          Working...