Results 1 to 5 of 5

Thread: varargs issue

  1. #1
    Qsc
    Qsc is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default varargs issue

    I thought I understood how it works after Junkey helped me out. Turns out I didn't. I typed this code in and got an error.

    Are you not allowed to use two varargs like that? If not, then I cannot use them with my constructors since I need to know the amount as well as the ingredient names.

    The orginal thread, with the conversation between Junkey and I is here. (If you need a reference to see what I was doing).

    Simple question

    Java Code:
    Recipe(String... args, int... args2) {
          
      }
    missing method body, or declare abstract

    ')' expected

    <identifier> expected

    cannot find symbol
    symbol: class args2

    <identifier> expected

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    My bad, I didn't fully explain. You can only use one varags in a parameter list and it must be the last parameter.

  3. #3
    Qsc
    Qsc is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Oh ok, I was kind of thinking that might of been so since I couldn't get it working. :S So I guess I am stuck with all those separate constructors then? Unless you know something super cool like those other two things. :).

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You could create an Ingredient class, create a bunch of Ingredient objects and pass them to the Recipe class.

  5. #5
    Qsc
    Qsc is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You could create an Ingredient class, create a bunch of Ingredient objects and pass them to the Recipe class.
    Oh..so simple its blindly obvious. ._. Thanks! I'd rep again but it wouldn't let me.

Similar Threads

  1. Same issue as before....
    By hayden06f4i in forum New To Java
    Replies: 4
    Last Post: 11-08-2010, 01:49 AM
  2. the strangeness of Varargs...
    By vlad in forum New To Java
    Replies: 6
    Last Post: 05-10-2010, 05:05 PM
  3. the strange and weird world of Varargs...
    By vlad in forum Advanced Java
    Replies: 2
    Last Post: 05-09-2010, 12:29 AM
  4. VarArgs Example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:38 PM

Posting Permissions

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