print("my name is {name}, and {age}-year old {gender}", name, age, gender);

Discussion in 'Java' started by =?iso-8859-1?B?bW9vcJk=?=, Dec 31, 2005.

  1. Hi,
    I want to make a print statement as the title appears. The point is,
    the print function can understand which variable corresponds to the
    desired parameter name inside the pattern.

    In C# print functions, the syntax is like
    print("my name is{0}, and {1}-year old {2}", name, age, gender);

    But I want to do a little further to make the print function knows
    which argument is going to fill the slot in the pattern string, even
    the arguments are not ordered in place:
    print("my name is {name}, and {age}-year old {gender}", age, gender,
    name);

    or it will have exception burst out when there is no suitable
    paramenter:
    print("hello, {name}", age);

    can we do that? Thx!
    =?iso-8859-1?B?bW9vcJk=?=, Dec 31, 2005
    #1
    1. Advertising

  2. =?iso-8859-1?B?bW9vcJk=?=

    VisionSet Guest

    "moopT" <> wrote in message
    news:...
    > Hi,
    > I want to make a print statement as the title appears. The point is,
    > the print function can understand which variable corresponds to the
    > desired parameter name inside the pattern.
    >
    > In C# print functions, the syntax is like
    > print("my name is{0}, and {1}-year old {2}", name, age, gender);
    >
    > But I want to do a little further to make the print function knows
    > which argument is going to fill the slot in the pattern string, even
    > the arguments are not ordered in place:
    > print("my name is {name}, and {age}-year old {gender}", age, gender,
    > name);
    >
    > or it will have exception burst out when there is no suitable
    > paramenter:
    > print("hello, {name}", age);
    >
    > can we do that? Thx!


    No because methods are predefined to take a fixed number of arguments.
    The closest you could get would be to overload a method with all the number
    of parameters you are likely to need and then you would have to have those
    parameter arguments as some Type probably Object.
    Objects toString() method could then be called to populate the message.

    But as to order? how? unless you used some type instead of Object that had a
    key field with which to match up the message tokens.

    public void print(String message, Object param1);

    public void print(String message, Object param1, Object param2);

    public void print(String message, Object param1, Object param2, Object
    param3);

    etc

    Now just implement them :)

    or you could do it with an array of parameters then you'd only need 1 method

    public void print(String message, Object[] params);

    eg

    print("My name is {name}, my age is {age}", new Param[] {new Param("age",
    "5"), new Param("name", "tom" )});

    --
    Mike W
    VisionSet, Dec 31, 2005
    #2
    1. Advertising

  3. =?iso-8859-1?B?bW9vcJk=?=

    Chris Smith Guest

    moop? <> wrote:
    > In C# print functions, the syntax is like
    > print("my name is{0}, and {1}-year old {2}", name, age, gender);


    See java.text.MessageFormat for a Java equivalent. Print
    (Writer/Stream).printf is also similar. The choice depends on what
    exactly you're trying to accomplish. MessageFormat tends to work better
    for abstract localization work, whereas the printf style syntax is
    better for precise formatting.

    > But I want to do a little further to make the print function knows
    > which argument is going to fill the slot in the pattern string, even
    > the arguments are not ordered in place:
    > print("my name is {name}, and {age}-year old {gender}", age, gender,
    > name);
    >
    > or it will have exception burst out when there is no suitable
    > paramenter:
    > print("hello, {name}", age);
    >
    > can we do that?


    No, you can't do that. The reason is that a method is not privy to the
    expressions that resulted in the actual parameters. In fact, that
    information isn't preserved past the compiler except in debugging info,
    so it can't possibly be available at runtime. As a side-note, I'm very
    glad that you can't do that. Do you really mean to make it impossible
    for someone to call that function unless they name their local variables
    as you want them to? That's sorta ridiculous.

    Note that you don't need to pass the parameters in order, though. Thus,
    you can say:

    > print("my name is {2}, and {0}-year old {1}", age, gender, name);


    You could also write an equivalent using a Map<String,Object> so that
    real names are associated with the objects instead of just local
    variable names.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Dec 31, 2005
    #3
  4. =?iso-8859-1?B?bW9vcJk=?=

    Stefan Ram Guest

    "moo" <> writes:
    >But I want to do a little further to make the print function knows
    >which argument is going to fill the slot in the pattern string, even
    >the arguments are not ordered in place:
    >print("my name is {name}, and {age}-year old {gender}", age, gender,
    >name);


    java.lang.System.out.println
    ( "My name is "+name+", and I am "+age+" years old and "+gender+"." );
    Stefan Ram, Dec 31, 2005
    #4
  5. =?iso-8859-1?B?bW9vcJk=?=

    Chris Smith Guest

    VisionSet <> wrote:
    > No because methods are predefined to take a fixed number of arguments.


    Presumably we're working in Java 1.5, so that's not true.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Dec 31, 2005
    #5
  6. =?iso-8859-1?B?bW9vcJk=?=

    VisionSet Guest

    "Chris Smith" <> wrote in message
    news:...
    > VisionSet <> wrote:
    > > No because methods are predefined to take a fixed number of arguments.

    >
    > Presumably we're working in Java 1.5, so that's not true.


    I'm constantly suprised by 1.5
    Does this feature have any real merit?

    --
    Mike W
    VisionSet, Dec 31, 2005
    #6
  7. =?iso-8859-1?B?bW9vcJk=?=

    Guest

    Mike,

    This feature (varargs) makes printf possible without this kind of code:

    printf("the format goes %s here",new Object[]{"stuff"});

    It makes the reflection APIs more comfortable (I don't use them
    anyway).

    It makes Arrays.asList more readable:

    List<String> strings=Arrays.asList("blah","blih","bloh");

    It has merit in terms of readability, but no, it is not absolutely
    required. It has a drawback too, from the called method's point of
    view, the arguments passed in might be of zero length, or might even be
    null, which is extra stuff to check for.

    int varArgMethod(int... numbers)
    {
    for (final int i: numbers)
    System.out.println(i);
    }

    This code could throw a NullPointerException (I think).

    Chris Smith should note the absence of a link to a certain article.
    , Dec 31, 2005
    #7
  8. =?iso-8859-1?B?bW9vcJk=?=

    Roedy Green Guest

    On Sat, 31 Dec 2005 11:12:05 GMT, "VisionSet" <>
    wrote, quoted or indirectly quoted someone who said :

    >I'm constantly suprised by 1.5
    >Does this feature have any real merit?


    which would you rather write:

    getMilkshakes( "chocolate", "strawberry","peach" );

    or

    getMilkshakes ( new String[] {"chocolate", "strawberry","peach"});
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 2, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cylin
    Replies:
    6
    Views:
    563
    Mike Wahler
    Aug 19, 2003
  2. keto
    Replies:
    0
    Views:
    896
  3. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    334
    David Cournapeau
    Dec 30, 2008
  4. Matthew Borgeson

    Newbie looking for help: Determining Gender

    Matthew Borgeson, Jul 18, 2007, in forum: Ruby
    Replies:
    11
    Views:
    190
    Chad Perrin
    Jul 19, 2007
  5. Replies:
    1
    Views:
    196
    John B. Matthews
    Jul 24, 2012
Loading...

Share This Page