4

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
3
  • You further ask about the following line of code: enterlessonnames(isimler[{"math","art"} ]); What do you expect to happen here? Could you provide the code for the method isimler? Commented Jul 18, 2013 at 9:13
  • Sorry it is names not isimler. Commented Jul 18, 2013 at 9:21
  • The best answer really has @NickJ Commented Jul 18, 2013 at 10:36

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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);

3 Comments

enterlessonnames( new String[]{"math", "art"} ); I could only call with that, do i need another array?
see my comment / question above
The array names is an argument of the method enterlessonnames and can only be used within the code of the method. If you are calling the method you can do either of the approaches shown in my answer. For the second approach a new array (lessons) is defined (and initialized) in the scope of the calling method. I recommend this intro regarding the array topic: docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html and this one regarding method calling: docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
2

In addition to the other answers, you could declare your method like this:

public void  enterlessonnames(String... names) {
  //do stuff
}

Then it can be called like this:

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

or like this:

enterlessonnames("just one string!");

or like this:

enterlessonnames("one string", "another string");  //as many strings as you like

Comments

1

Call it as:

public class ArrayCaller{
    public static void main(final String[] args) {
        new ArrayCaller().enterlessonnames(new String[]{"lesson1", "lesson2", "lesson3"});
    }

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

        for (int i=0;i<names.length;i++) {
            msg=msg+names[i];
        }
        System.out.println(msg);
    }
}

Cheers!!

Comments

1

Probably what you are looking for is invoking like this:

enterlessonnames(new String[] {"CursedChico","Science","Maths"});

Keep in mind that newly created array will be disposed and won't be available to re-use in an other method.

If you are not enforced, I can suggest you to use generics like;

List<String> names= new ArrayList<String>();
names.add("Math");
names.add("Science");

etc..

And you can modify your method as;

public void enterLessonNames(List<String> names)    
{
   Here goes your code;
}

Afterwards invoking;

enterLessonNames(names);

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.