Question on formatting Strings

J

Joona I Palaste

Would there be any existing way in Java 1.4.2 to do this kind of
conversion?

Input: A reference to a java.lang.Object (or to any subclass of it)
Output: If the reference is not null, a concatenated String of the
following:
" (i.e. a double quote)
the result of the object's toString() method
" (i.e. another double quote)
If the reference is null, the String null (i.e. a String consisting of
four characters n, u, l, l, no double quotes).

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
C

Chris Smith

Joona said:
Would there be any existing way in Java 1.4.2 to do this kind of
conversion?

Input: A reference to a java.lang.Object (or to any subclass of it)
Output: If the reference is not null, a concatenated String of the
following:
" (i.e. a double quote)
the result of the object's toString() method
" (i.e. another double quote)
If the reference is null, the String null (i.e. a String consisting of
four characters n, u, l, l, no double quotes).

Existing? The code isn't already written, but it should be rather
trivial to write:

String out = (in == null) ? "null" : "\"" + in + "\"";

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Joona I Palaste

Existing? The code isn't already written, but it should be rather
trivial to write:
String out = (in == null) ? "null" : "\"" + in + "\"";

Yes, I know. Point is, I would like to write this in a veritable
cornucpia (in Layman: a lot) of places in our application's source code.
It would be *that* much easier if there was a ready-made method call.
Thank anyway. Maybe I'll write my own utility method.
 
P

Paul Lutus

Joona I Palaste wrote:

Yes, I know. Point is, I would like to write this in a veritable
cornucpia (in Layman: a lot) of places in our application's source code.
It would be *that* much easier if there was a ready-made method call.
Thank anyway. Maybe I'll write my own utility method.

Create a static class that has this function as its only member. Put the
class in your project classpath. Refer to it at will.

final public class Gadget {
public static void doItForMe(Object ob)
{
System.out.println(ob == null ? "null" : "\"" + ob + "\"");
}
}

Gadget.doItForMe(null);

null

Gadget.doItForMe(new Object());

"java.lang.Object@14fe5c"
 

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

Similar Threads


Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top