Argument that references an array of type String
Hello
I'm trying to use an array of type String as method argument (see below)
public void buildFrom(String[] wordList)
{
for(String word1: wordList)
{
System.out.println(word1);
}
}
but when I try to send the message with the strings I get the following semantic error message
WordSearchMaker wsm = new WordSearchMaker();
wsm.builFrom("JAVA", "PUBLIC", "OBJECT", "METHOD", "INT", "CLASS",
"PRIVATE", "STATIC", "FLOAT", "SUPER", "PROTECTED", "THIS",
"IMPORT", "FINAL");
error message:
Semantic error: line 1. Message builFrom( java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String ) not understood by class'WordSearchMaker'
Any help will be greatly appreciated
Thanks
Re: Argument that references an array of type String
You pass many strings to that method, but not an array!
buildFrom(new String[]{"JAVA", .......});
or you have to use varargs!
change your method signature to public void buildFrom(String... wordList)
Re: Argument that references an array of type String
Hi
I need to write a public instance method called buildFrom() which takes a single
argument called wordList that references an array of type String and returns
no result.
So I must have the possibility to change the strings sending the message to wsm anytime i want. I can't change the method's argument from array String to String.
I'm testing the method in BlueJ.
Thanks
Re: Argument that references an array of type String
Quote:
Originally Posted by
semio
Hi
I need to write a public instance method called buildFrom() which takes a single
argument called wordList that references an array of type String and returns
no result.
So I must have the possibility to change the strings sending the message to wsm anytime i want. I can't change the method's argument from array String to String.
I'm testing the method in BlueJ.
Thanks
You don't understand the previous reply; this method can take a variable number of Strings as well as a String array:
Code:
T yourMethod(String ... args) { ... }
and this method can only take a String array as its (single) argument:
Code:
T yourMethod(String[] args) { ... }
kind regards,
Jos
Re: Argument that references an array of type String
being a newbie I have looked at semio code and presumed eveything would work, reading the responses Im a little confused can you guys elaborate?