-1

I'm getting the error "* Operator Undefined for JTextField, Double" in the listener of a project. It's a Java GUI project, we're required to have two text fields, one for "hours" the other "rate". We are also required to have a combo box listing employee type and a calculation button. I'm getting the error in switch case 2 (pay = hours * payRate;) under the listener. How do I convert the text so I can multiply hours by payRate?

public class EmployeeControls extends JPanel
{
private JLabel cost, inputLabel1, inputLabel2, outputLabel, resultLabel;
private JTextField hours, rate;
private JComboBox employeeCombo;
private JButton calculateButton;
private double pay, bonus, payRate;

public EmployeeControls()
{   
    inputLabel1 = new JLabel("Hours:");
    inputLabel2 = new JLabel("Rate:");
    outputLabel = new JLabel("Pay:");
    resultLabel = new JLabel("------");
    hours = new JTextField(2);
    rate = new JTextField(5);
    hours.addActionListener(new CalcListener());
    rate.addActionListener(new CalcListener());

    String[] employeeTypes = {"Select an Employee 
    Type","Salaried","Hourly","Volunteer"};


    employeeCombo = new JComboBox(employeeTypes);

    calculateButton = new JButton("Calculate Pay");

    cost = new JLabel("Pay: " + pay);

    setPreferredSize(new Dimension (400, 100));
    setBackground(Color.cyan);

    add(employeeCombo);
    add(calculateButton);

    calculateButton.addActionListener(new CalcListener());

   }

    private class CalcListener implements ActionListener
   {
    public void actionPerformed (ActionEvent event)
    {

        String text = rate.getText();
        String strRate = Double.toString(payRate);

        String text2 = hours.getText();
        String strHours = Double.toString(hours);


        int employeeType = employeeCombo.getSelectedIndex();

        switch(employeeType)
        {
            case 0:
                JOptionPane.showMessageDialog(null, "Select an Employee 
       Type");
                break;
            case 1:
                pay = (2000.00 + bonus);
                JOptionPane.showMessageDialog(null, "Enter bonus amount");
                break;
            case 2:
                pay = hours * payRate;
                break;
            case 3:
                pay = 0.00;
                break;

        }
        cost.setText("Cost = " + pay);
     }

     }
     }
lorac1969
  • 1
  • 1
  • 7
  • 2
    `pay = hours * payRate` - `hours` is a `JTextField`, how can you multiple it by a double? – MadProgrammer May 11 '17 at 22:44
  • `String strHours = Double.toString(hours);` is also going to give you issues for simular reasons – MadProgrammer May 11 '17 at 22:45
  • @MadProgrammer we're required to use a textfield, a payrate needs to have a decimal so I don't beleive I can use integer. What type would you use? – lorac1969 May 11 '17 at 23:47
  • `Double.toString(hours)` would require a `double` value. I think all of your conversions are backwards, you want to take a `String` and parse it to `double`, so that would mean you need to get the `String` text from the text field first – MadProgrammer May 11 '17 at 23:56

1 Answers1

3

You are trying to multply the JTextField: it doesn't make sense.

To fix, get the value of the text field with hours.getText() and then parse it to int with Integer.parseInt(hours.getText()). So it becomes :

pay = Integer.parseInt(hours.getText()) * payRate
ollaw
  • 2,086
  • 1
  • 20
  • 33
  • Hi Ollaw, thanks for the tip. We are required to get the payrate from a text field, how would you write it? – lorac1969 May 11 '17 at 23:48
  • the same way, get the text of the textField with .getText(), but this method returns a String, so you cast it to the type you want, for example with int you need to do Integer.parseInt( value ), with double Double.parseDouble(value) and so on – ollaw May 12 '17 at 09:16