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!
VisionSet - 31 Dec 2005 01:59 GMT
> Hi,
> I want to make a print statement as the title appears. The point is,
[quoted text clipped - 15 lines]
>
> 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
Chris Smith - 31 Dec 2005 02:30 GMT
> 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.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
VisionSet - 31 Dec 2005 11:12 GMT
> > 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
ricky.clarkson@gmail.com - 31 Dec 2005 12:57 GMT
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.
Roedy Green - 02 Jan 2006 16:39 GMT
>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"});

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Chris Smith - 31 Dec 2005 02:15 GMT
moop? <samhng@gmail.com> 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
[quoted text clipped - 7 lines]
>
> 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.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Stefan Ram - 31 Dec 2005 02:15 GMT
"moo" <samhng@gmail.com> 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+"." );