8 Replies - 1129 Views - Last Post: 03 June 2013 - 06:50 AM Rate Topic: -----

#1 torosyana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-June 13

Java: Cannot find symbol (Help)

Posted 02 June 2013 - 03:50 PM

I need help with one of my previous Java code for a payroll calculator. Receive an error for the highlighted variable inside the try statement along with other similar variables. Can some explain what needs to be done for the variable to be located. Here is the error I receive:

cannot find symbol
symbol: variable hoursTextField
location: class Payroll.CalcButtonListener


/**
 * This GUI-based program displays a payroll application.
 * The application calculates employee pay when the user inputs the required information and clicks the calculate button.
 * Overtime is hours worked over 40 with rate 1.5 times the pay rate. 
 * Application exits when user clicks the exit button.
 * 
 * @author atorosyan
 */
                
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.table.*;
import javax.swing.table.DefaultTableColumnModel.*;
import java.io.*;

public class Payroll extends JFrame
{
    private final int WINDOW_WIDTH = 900;    // Window width in pixels
    private final int WINDOW_HEIGHT = 500;   // Window height in pixels
    double hours;
    double rate;
    double pay;
    private DefaultTableModel model;
    private JTable table;
    private JScrollPane scroll;
    
private String[] department = { "IT", "Marketing",  // Dropdown menu 
                                   "Accounting", "Sales", "HR"};
   
public Payroll (){                        // Constructor
    super("Payroll Application");            // Window name
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);    // Set the size of the window.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
         
    setLayout (new GridLayout(4, 0));
    setLayout (new BorderLayout());

    
 // Create the label components   
JLabel greeting = new JLabel ("Payroll Application");
JLabel nameLabel = new JLabel ("Employee Name");  
JLabel hoursLabel = new JLabel ("Hours Worked");
JLabel rateLabel = new JLabel ("Pay Rate");
JLabel payLabel = new JLabel ("Pay");
JComboBox departmentBox = new JComboBox(department);
JButton calcButton = new JButton ("Calculate Pay");
JButton exitButton = new JButton ("Exit");
JTextField nameTextField = new JTextField(15);
JTextField hoursTextField = new JTextField(5);
JTextField rateTextField = new JTextField(5);
JTextField payTextField = new JTextField (10);
payTextField.setEditable(false);
model = new DefaultTableModel();
table = new JTable(model);

model.addColumn("Name");
model.addColumn("Age");
model.addColumn("E-mail");
model.addColumn("Cell Phone");

scroll = new JScrollPane
(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setPreferredScrollableViewportSize
        (new Dimension (700,200));
TableColumn column = null;
for (int i = 0; i < 4; i++)
{
    column = table.getColumnModel
        ().getColumn(i);
if (i==0)
{
    column.setPreferredWidth
(30);
    }if (i==1 || i==4)
{
    column.setPreferredWidth
(30);
}else
{
    column.setPreferredWidth
(30);
}}


// Create the panels to hold the components
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();


panel1.add(greeting);
panel2.add(nameLabel);            
panel2.add(nameTextField);
panel2.add(departmentBox);
panel2.add(hoursLabel);
panel2.add(hoursTextField);
panel2.add(rateLabel);
panel2.add(rateTextField);
panel5.add(calcButton);
panel5.add(exitButton);
panel6.add(payLabel);
panel6.add(payTextField);

add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.WEST);
add(panel3, BorderLayout.CENTER);
add(panel4, BorderLayout.EAST);
add(panel5, BorderLayout.SOUTH);
add(panel6, BorderLayout.EAST);

calcButton.addActionListener(new Payroll.CalcButtonListener());
exitButton.addActionListener(new Payroll.ExitButtonListener());
   
   hoursTextField.setText("0");
   rateTextField.setText("0");

   setVisible(true); //Display window 
     
//Calculate pay
}
private class CalcButtonListener implements ActionListener 
{
    public void actionPerformed(ActionEvent e) 
    {
    
        
   try{
       String hours;
       String rate;
       double pay;
       hours = hoursTextField.getText();
       rate = rateTextField.getText();

if (Double.parseDouble(hours) <= 40)
pay = Double.parseDouble(hours) * Double.parseDouble(rate);
else
pay = ((Double.parseDouble(hours) - 40) * (Double.parseDouble(rate) * 1.5) + 
        Double.parseDouble(rate) * 40); 

payTextField.setText("$"+pay);

model.addRow(new Object[]{nameTextField.getText(),
    departmentBox.getSelectedItem().toString(),payTextField.getText()});
   
   

if (nameTextField.getText().isEmpty() || departmentBox.getSelectedItem().isEmpty()|| 
        payTextField.getText().isEmpty())
        throw new IllegalArgumentException();    


   }catch (IllegalArgumentException empty) {
    JOptionPane.showMessageDialog(null, "One of the required fields is empty!", 
            "Error", JOptionPane.ERROR_MESSAGE);}
   
    }
}

   private class ExitButtonListener implements ActionListener 
{
        
    public void actionPerformed(ActionEvent e) 
    {

     System.exit(0);            // Close the program.
       
    }
   }


public  static void main (String[] args) 
{
    
    Payroll program = new Payroll(); 
}

}


This post has been edited by macosxnerd101: 02 June 2013 - 04:22 PM
Reason for edit:: Please use code tags around all your code.


Is This A Good Question/Topic? 0
  • +

Replies To: Java: Cannot find symbol (Help)

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,483
  • Joined: 12-December 12

Re: Java: Cannot find symbol (Help)

Posted 02 June 2013 - 04:43 PM

It would help very much if you told us which line in your posted code the error occurs on.

It would also help, very much, if the code was properly indented. It is hard to tell which code belongs to which method (or class, or neither) which is probably the root of the problem.

This post has been edited by andrewsw: 02 June 2013 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,483
  • Joined: 12-December 12

Re: Java: Cannot find symbol (Help)

Posted 02 June 2013 - 04:55 PM

Anyways.. you've declared a lot of variables (in addition to hoursTextField) in your constructor. These are local to the constructor and not available to other methods or events.
Was This Post Helpful? 0
  • +
  • -

#4 torosyana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-June 13

Re: Java: Cannot find symbol (Help)

Posted 02 June 2013 - 04:59 PM

Hello and thank you for the reply. The line numbers are Line 138,139, 147, 149, 154

All same error message for the variable.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,483
  • Joined: 12-December 12

Re: Java: Cannot find symbol (Help)

Posted 02 June 2013 - 05:11 PM

See my previous post. If these variables need to be available throughout the Class then they could be declared at the class-level (as fields).

This post has been edited by andrewsw: 02 June 2013 - 05:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 torosyana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-June 13

Re: Java: Cannot find symbol (Help)

Posted 02 June 2013 - 05:32 PM

I believe they are listed as fields but seem they are not available to be located in the code where indicated. Do you have a IDE like Netbeans to test the code?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,483
  • Joined: 12-December 12

Re: Java: Cannot find symbol (Help)

Posted 02 June 2013 - 05:41 PM

View Posttorosyana, on 03 June 2013 - 12:32 AM, said:

I believe they are listed as fields but seem they are not available to be located in the code where indicated. Do you have a IDE like Netbeans to test the code?

Yes, I have NetBeans, but I don't need to test the code; I've already suggested a solution. These variables are not "listed as fields" (according to the code you have posted), they are local variables within your constructor.
Was This Post Helpful? 0
  • +
  • -

#8 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: Java: Cannot find symbol (Help)

Posted 02 June 2013 - 05:52 PM

View Posttorosyana, on 03 June 2013 - 12:32 AM, said:

I believe they are listed as fields but seem they are not available to be located in the code where indicated. Do you have a IDE like Netbeans to test the code?


Listen to what andrewsw is saying. You can't call variables outside of the constructor if it is declared within the constructor. All of your component declarations need to be taken out of

public Payroll() {

}



do something like,

public class Payroll extends JFrame {

    JLabel label = new JLabel("label");

    public Payroll() {
        //....
        add(label);
    }

}


Was This Post Helpful? 0
  • +
  • -

#9 torosyana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-June 13

Re: Java: Cannot find symbol (Help)

Posted 03 June 2013 - 06:50 AM

Thank you andrewsw and schutzzz for your help. Your feedback helped to locate the variables from the class. Appreciate the support
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1