interface Interface1
{
  public int method1();
  public int method2();
}
public class Salary implements Interface1
{
   public static void main(String args[])
  {
    int sa[]=Integer.parseInt(arg[0]);
    float pf,hra,da,tax,net;
    public int method1()
    {
     System.out.println("salary="+sa);
     try{
     if(sa>250000)
     {
      pf=sa*5/100;
      hra=sa*10/100;
      da=sa*8/100;
      tax=sa*10/100;
      net=sa+tax;
      System.out.println("Net salary of an employee"+net);
     }
     }
     catch(Exception e)
     {
       System.out.println("I/O error");
     }
    }
    public int method2()
    {
      System.out.println("salary="+sa);
      if(sa>500000)
      {
       pf=sa*5/100;
       hra=sa*10/100;
       da=sa*8/100;
       tax=sa*20/100;
       net=sa+tax;
       System.out.println("Net salary of an employee"+net);
     }
     else
     {
       System.out.println("No deduction");
     }
  }
  Salary s=new Salary();
    s.method1();
    s.method2();
 }
}

I'm getting 4 errors in this program....and they are

line no.12 : illegal start of expression
line no.12 : ';' expected
line no.31 : illegal start of expression
line no.31 : ';' expected

Recommended Answers

All 7 Replies

You try to define method1 but you are still inside the main method. You ca't define a method inside another method.

You can't have method in method.
method1() and method2() are in public static void main() so compailer is complaining...
put method1 and method2 outside public static void main and it will be ok.

also: avoid doing this

catch(Exception e)
     {
       System.out.println("I/O error");
     }

an Exception isn't necessarily an I/O error. print the stacktrace instead, at least that 'll tell you what is wrong, and where it went wrong. the above will just print a vague, and possibly totally wrong error message.

First you cannot define method in the static void main() method, so method1 and method2 should be in the class Salry but not into main.
Second method1 and method2 should return an interger which you didn't in your exemple, so you need to add a return statement or to change the methods so it returns nothing (from int to void)
Third the try catch exception isnt important because the method1 doesnt throw exception, so no need for try catch.

so the code should be like this, i hope this help.

public class Salary implements Interface1
{
    float pf,hra,sa,da,tax,net;

    public Salary(int sa)
    {
        this.sa = sa;
    }

    public void method1()
    {
        System.out.println("salary="+sa);

        if(sa>250000)
        {
            pf=sa*5/100;    
            hra=sa*10/100;
            da=sa*8/100;
            tax=sa*10/100;
            net=sa+tax;
            System.out.println("Net salary of an employee"+net);
        }
    }

    public void method2()
    {
        System.out.println("salary="+sa);
        if(sa>500000)
        {
            pf=sa*5/100;
            hra=sa*10/100;
            da=sa*8/100;
            tax=sa*20/100;
            net=sa+tax;
            System.out.println("Net salary of an employee"+net);
        }
        else
        {
            System.out.println("No deduction");
        }
    }

    public static void main(String args[])
    {
        int sa = Integer.parseInt(args[0]); 
        Salary s=new Salary(sa);
        s.method1();
        s.method2();
    }
}

I'm sure your getting the point by now, moving your methods outside of main will solve your problem. But you will also need to move your variable declarations as hannahaddad showed in his code, you can declare variables in a method or make them global as shown in the example above.

beautifuldonkey: try not to confuse the OP, no matter how well you mean it. there are no such things as 'global variables' in Java.

thanx "hannahaddad"
now i got my answer :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.