Hello fellow developers,

Does anyone know why I keep getting this error message "illegal start of expression"? I've already checked my braces. They matched. Still.....[This is code for the basic operation of a calculator n my code is as follows]

public void button2_actionPerformed(ActionEvent e) {
      applyOperator('+');
   }

   public void button14_actionPerformed(ActionEvent e) {
      applyOperator('-');
   }

   public void button13_actionPerformed(ActionEvent e) {
      applyOperator('*');
   }

   public void button12_actionPerformed(ActionEvent e) {
      applyOperator('/');
   }

   private void applyOperator(String theOperator){
   
       if (theOperator == '+'){
           public void actionPerformed_Plus(){   //Illegal start of expression here
               if (firstTime){
                   String firstNumber;
                   firstNumber = textField1.getText();
                   Double numberDouble1 = Double.valueOf(firstNumber);
                   number1 = numberDouble1.doubleValue();
                   result = result + number1;
                   String displayResult = String.valueOf(result);
                   textField1.setText(displayResult);
                   operator = '+';}
                else{
                   String firstNumber;
                   firstNumber = textField1.getText();
                   Double numberDouble1 = Double.valueOf(firstNumber);
                   number1 = numberDouble1.doubleValue();
                   textField1.setText(firstNumber);
                   operator = '+';
                   firstTime = false;}
           }
       }else if(theOperator=='-'){
           public void actionPerformed_Minus() { //Illegal start of expression here
               if (firstTime) {
                   String firstNumber;
                   firstNumber = textField1.getText();
                   Double numberDouble1 = Double.valueOf(firstNumber);
                   number1 = numberDouble1.doubleValue();
                   result = result - number1;
                   String displayResult = String.valueOf(result);
                   textField1.setText(displayResult);
                   operator = '-';
               } else {
                   String firstNumber;
                   firstNumber = textField1.getText();
                   Double numberDouble1 = Double.valueOf(firstNumber);
                   number1 = numberDouble1.doubleValue();
                   textField1.setText(firstNumber);
                   operator = '-';
                   firstTime = false;
        } }   //Illegal start of expression here     
     }else if(theOperator =='*'){
           public void actionPerformed_Multiply() {
               if (firstTime){
                       String firstNumber;
                       firstNumber = textField1.getText();
                       Double numberDouble1 = Double.valueOf(firstNumber);
                       number1 = numberDouble1.doubleValue();
                       result = result * number1;
                       String displayResult = String.valueOf(result);
                       textField1.setText(displayResult);
                       operator = '*';}      
               else{
                       String firstNumber;
                       firstNumber = textField1.getText();
                       Double numberDouble1 = Double.valueOf(firstNumber);
                       number1 = numberDouble1.doubleValue();
                       textField1.setText(firstNumber);
                       operator = '*';
                       firstTime = false; }
            }
    }else if(theOperator == '/'){
            public void actionPerformed_Divide() { //Illegal start of expression here
                   if (firstTime) {
                       String firstNumber;
                       firstNumber = textField1.getText();
                       Double numberDouble1 = Double.valueOf(firstNumber);
                       number1 = numberDouble1.doubleValue();
                       result = result / number1;
                       String displayResult = String.valueOf(result);
                       textField1.setText(displayResult);
                       operator = '/';  } 
                   else{
                       String firstNumber;
                       firstNumber = textField1.getText();
                       Double numberDouble1 = Double.valueOf(firstNumber);
                       number1 = numberDouble1.doubleValue();
                       textField1.setText(firstNumber);
                       operator = '/';
                       firstTime = false; }
           }
    }else{
          result = number1; }
          return result;  
  }
}

Thanks in advance.

You cannot declare a method within a method like that, which is what this line is trying to do

public void actionPerformed_Plus()
Comments
Promply helpful

Also, consider you can use a switch in applyOperation()

switch (op){
    case '+':
        // stuff
        break;

    case '-':
        //stuff
        break;

}

Another possibility is to define your own Actions for each of those buttons

class PlusAction extends AbstractAction{
        public void actionPerformed(ActionEvent e) {
            System.out.println("do plus action");
        }
    }

and set that action on the button with

button2.setAction(new PlusAction());

(edit: You still really need to ditch the "button2", "button_3" variable names and make them "btnPlus" or "buttonPlus" or something. Meaningful variable names go a long way towards making your code easier to understand - not just for others, but also for yourself)

This code is illegal. You can not call functions this way.

if (theOperator == '+'){
       public void actionPerformed_Plus(){   //Illegal start of expression here
           if (firstTime){
               String firstNumber;
               firstNumber = textField1.getText();
               Double numberDouble1 = Double.valueOf(firstNumber);
               number1 = numberDouble1.doubleValue();
               result = result + number1;
               String displayResult = String.valueOf(result);
               textField1.setText(displayResult);
               operator = '+';}
            else{
               String firstNumber;
               firstNumber = textField1.getText();
               Double numberDouble1 = Double.valueOf(firstNumber);
               number1 = numberDouble1.doubleValue();
               textField1.setText(firstNumber);
               operator = '+';
               firstTime = false;}
       }
   }

Instead create a new function with operators as Parameters.

public void button2_actionPerformed(ActionEvent e) {
      calculate("+");
}

private void calculate( String operator)
{
       if(operator.equals("+"){
             String firstNumber;
             firstNumber = textField1.getText();
             Double numberDouble1 = Double.valueOf(firstNumber);
             number1 = numberDouble1.doubleValue();
              result = result + number1;
              String displayResult = String.valueOf(result);
              textField1.setText(displayResult);
       }
       else if(operator.equals("-")){
              String firstNumber;
             firstNumber = textField1.getText();
             Double numberDouble1 = Double.valueOf(firstNumber);
             number1 = numberDouble1.doubleValue();
              result = result  - number1;
              String displayResult = String.valueOf(result);
              textField1.setText(displayResult);
        }
        and so on.
}

This will work.

Edited 3 Years Ago by pyTony: fixed formatting

This question has already been answered. Start a new discussion instead.