Results 1 to 5 of 5
  1. #1
    semio is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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)
    Last edited by eRaaaa; 03-10-2012 at 12:27 PM.

  3. #3
    semio is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Argument that references an array of type String

    Quote Originally Posted by semio View Post
    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:

    Java Code:
    T yourMethod(String ... args) { ... }
    and this method can only take a String array as its (single) argument:

    Java Code:
    T yourMethod(String[] args) { ... }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    trying is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default 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?

Similar Threads

  1. passing an array as argument
    By samanesh in forum New To Java
    Replies: 1
    Last Post: 02-29-2012, 03:10 PM
  2. operator undefined for argument type int, String
    By aconti in forum New To Java
    Replies: 8
    Last Post: 05-30-2011, 07:32 AM
  3. N dimensional array as argument?
    By foota in forum Advanced Java
    Replies: 16
    Last Post: 05-07-2011, 09:55 AM
  4. Array of references?
    By w00tguy123 in forum New To Java
    Replies: 8
    Last Post: 04-06-2011, 06:37 AM
  5. Secret of String references comparision.
    By Anjaneyulu in forum Advanced Java
    Replies: 15
    Last Post: 02-24-2010, 02:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •