Forum Stats

  • 3,827,840 Users
  • 2,260,828 Discussions
  • 7,897,397 Comments

Discussions

I can't figure out how to fix this to run the program

a08ac898-68a5-497b-8fce-0e9aef938316
edited Jan 3, 2016 9:21PM in New To Java

Hello everyone!

I am almost ready to run this program and I cannot find out how to fix the error in my code. It says "Illegal start of an expression" I don't know how to fix it.

/**

*

* @author James

*/

public class NewMain {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

  public class SalesPerson {

// create variable (fixedSalary)

double fixedSalary;

// variable of the value of sale person's annual sales

double annualSales;

//commission that is earned

double commission;

//The target for sales that must be reached by sales person

double target;

public SalesPerson(double annualSales) {

  this.annualSales = annualSales;

  target = 140000;

  commission = 0;

  if (annualSales > target * 0.8) {

   if (annualSales < target) commission = 0.25 * annualSales; //The current commission 25% of total sales.

   else commission = 0.25 * 1.25 * annualSales; //The current commission (0.25*1.25)% of total sales.

  }

  fixedSalary = 75000; // set fixed salary is 75000$

}

public double getTotalAnnualCompensation() { // calculate The total annual compensation is the fixed salary plus the commission earned

  return fixedSalary + commission;

}

}

    }

   

}

Answers

  • Unknown
    edited Dec 30, 2015 11:14AM
    I am almost ready to run this program and I cannot find out how to fix the error in my code. It says "Illegal start of an expression" I don't know how to fix it.
    

    Well first you have to FIND the problem.

    Remove (comment out) half the code and see if it works. Keep removing code until it works. Then you know the problem is in what you removed.

    So add code back in small pieces until it does NOT work. Then you know the problem is in what you just added back.

    Keep going back and forth until you identity the code that causes the problem

    I am almost ready to run this program and I cannot find out how to fix the error in my code. It says "Illegal start of an expression" I don't know how to fix it.
    
      if (annualSales > target * 0.8) {
       if (annualSales < target) commission = 0.25 * annualSales; //The current commission 25% of total sales.
       else commission = 0.25 * 1.25 * annualSales; //The current commission (0.25*1.25)% of total sales.
      }
      fixedSalary = 75000; // set fixed salary is 75000$
    }
    

    Do you see ANYTHING wrong there?

    How can annualSales be both BIGGER than the target (1st line) and LESS THAN the target (second line)?

    Count the number of left and right braces and see if they match.

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Dec 30, 2015 1:34PM
    a08ac898-68a5-497b-8fce-0e9aef938316 wrote:
    /**
    *
    * @author James
    */
    public class NewMain {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
      public class SalesPerson {
    

    Here you create an inner class within a method. This is only possible if you omit the access modifier (the "public" keyword).

    Nevertheless I doubt that this was intended so you should move the complete class Person before the declaration of the "main" method (and its comment) or even better to a file of its own.

    bye

    TPD

  • venkatasundar
    venkatasundar Member Posts: 17
    edited Dec 30, 2015 10:50PM
    rp0428 wrote:
    
    
    I am almost ready to run this program and I cannot find out how to fix the error in my code. It says "Illegal start of an expression" I don't know how to fix it.
    
    Well first you have to FIND the problem.
    
    Remove (comment out) half the code and see if it works. Keep removing code until it works. Then you know the problem is in what you removed.
    
    So add code back in small pieces until it does NOT work. Then you know the problem is in what you just added back.
    
    Keep going back and forth until you identity the code that causes the problem
    
    
    I am almost ready to run this program and I cannot find out how to fix the error in my code. It says "Illegal start of an expression" I don't know how to fix it.
    
      if (annualSales > target * 0.8) {
       if (annualSales < target) commission = 0.25 * annualSales; //The current commission 25% of total sales.
       else commission = 0.25 * 1.25 * annualSales; //The current commission (0.25*1.25)% of total sales.
      }
      fixedSalary = 75000; // set fixed salary is 75000$
    }
    
    Do you see ANYTHING wrong there?
    
    How can annualSales be both BIGGER than the target (1st line) and LESS THAN the target (second line)?
    
    Count the number of left and right braces and see if they match.
    
    
    

    As per the program

    To get a commission, the annualSales should greater than 80% of the target ( i.e to pass the first condition )

    The second condition is when annualSales is greater than 80% but less than actual target

    and the else part is for annualSales greater than or equal to target.

    There is no issue in this.

    as pointed by TPD-Opitz removing the "public" modifier for the class "SalesPerson" would be suffix for the clearing compilation error.

  • Devang Adeshara
    Devang Adeshara Member Posts: 1
    edited Dec 31, 2015 4:49AM

    Hi,

      You program have few error while declaring SalesPerson class.

    Mistake 1 ) You can not declare any class inside the main method. It must be OUTSIDE of the main method.

    Mistake 2) You have to add static keyword while declaring SalesPerson class.

       Additionally i have created one instance of SalesPerson and findout the annual commision

    ================================================================================

    /**

    *

    *

    *

    * @author James

    */

    public class NewMain {

        /**

         *

         * @param args

         *            the command line arguments

         */

        public static void main(String[] args) {

            SalesPerson salesPerson1 = new SalesPerson(10000);

            System.out.println("Sales Person Final Commission : " + salesPerson1.getTotalAnnualCompensation());

           

        }

        public static class SalesPerson {

            // create variable (fixedSalary)

            double fixedSalary;

            // variable of the value of sale person's annual sales

            double annualSales;

            // commission that is earned

            double commission;

            // The target for sales that must be reached by sales person

            double target;

            public SalesPerson(double annualSales) {

                this.annualSales = annualSales;

                target = 140000;

                commission = 0;

                if (annualSales > target * 0.8) {

                    if (annualSales < target)

                        commission = 0.25 * annualSales; // The current commission

                                                            // 25% of total sales.

                    else

                        commission = 0.25 * 1.25 * annualSales; // The current

                                                                // commission

                                                                // (0.25*1.25)% of

                                                                // total sales.

                }

                fixedSalary = 75000; // set fixed salary is 75000$

            }

            public double getTotalAnnualCompensation() { // calculate The total

                                                            // annual compensation

                                                            // is the fixed salary

                                                            // plus the commission

                                                            // earned

                return fixedSalary + commission;

            }

        }

    }

  • venkatasundar
    venkatasundar Member Posts: 17
    edited Jan 3, 2016 9:21PM

    Nothing wrong in the code, except the class "SalesPerson" shouldn't be public.

    the following code, at the end of main method (before closing the main method) will do, to display the total annual compensation:

        SalesPerson s = new SalesPerson(140000);

        double comp = s.getTotalAnnualCompensation();

        System.out.println(comp); // just printed on screen. the variable "comp" has necessary value.

This discussion has been closed.