Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java invoking a method with array parameter

I wrote the following function:

public void enterlessonnames(String[] names)
        {
            String msg="";

            for (int i=0;i<names.length;i++)
            {

                msg=msg+names[i];
            }

            System.out.println(msg);
 }

I want to call like that, giving the input:

enterlessonnames({"math","art"} );

How can i call this in main?

enterlessonnames(names[{"math","art"} ]);

It does not any of them.

Multiple markers at this line:

- Syntax error, insert ")" to complete MethodInvocation
- Syntax error on token ",", delete this token
- Syntax error, insert ";" to complete Statement
- Syntax error on tokens, delete these tokens
like image 301
CursedChico Avatar asked Dec 26 '22 00:12

CursedChico


2 Answers

like this:

enterlessonnames( new String[] { "a", "b" } );

FYI, java naming conventions imply that method names have first letter of each word in the name start with a capital letter except for the first word that starts with non-capital. In your case: enterLessonNames.

like image 148
yair Avatar answered Dec 28 '22 14:12

yair


You need to create a proper String array instance, something like this:

String[] array = new String[]{"math", "art"};

Your fixed call would be:

enterlessonnames( new String[]{"math", "art"} );

or

String[] lessons = new String[]{"math", "art"};
enterlessonnames(lessons);
like image 37
reto Avatar answered Dec 28 '22 14:12

reto