user2423959
user2423959

Reputation: 834

three weird errors seen for the first time

I've been working on arrays and when i declared a method, it is giving some 3 errors, please help me to fix them.

My code is

public class Dummy {
    public static void main(String args[]) throws Exception {
         Random rand=new Random();

         int[] a={1,3,-3};
         int[] b={2,1,-4};


         public int sum()    {

         }
     }
}

and the errors i get are

Syntax error on token "int", @ expected
Syntax error, insert "enum Identifier" to complete EnumHeaderName
Syntax error, insert "EnumBody" to complete BlockStatement

i tried some alternatives by seeing similar errors reported, but didn't find any other solution

Thanks

Upvotes: 0

Views: 53

Answers (2)

wonhee
wonhee

Reputation: 1661

  1. sum() should be defined outside of main()
  2. you should either define sum() as static so you can call Dummy.sum() inside of main or instantiate Dummy and call dummy.sum() or something. However, I don't see any relevant code in your main() so I don't know what you are tying to. Wanna call like

    Dummy.sum(a);

    Dummy.sum(b);

? then sum() should be static and take int[] as parameter. 3. I don't think the error message you posted is related to your code. It comes from somewhere else. Do you have other classes defined in the same project or classpath? ( Like multiple java file in same project of your Eclipse?)

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

You can't declare a method (sum()) inside another method (main()). A method must be in a class.

Upvotes: 5

Related Questions