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

  • Thread starter =?iso-8859-1?B?bW9vcJk=?=
  • Start date
?

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

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!
 
V

VisionSet

moopT said:
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" )});
 
C

Chris Smith

moop? said:
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
 
S

Stefan Ram

moo said:
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+"." );
 
C

Chris Smith

VisionSet said:
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
 
R

ricky.clarkson

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.
 
R

Roedy Green

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"});
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top