English 中文(简体)
Smart Java formatter
原标题:

I m looking for a smart Java String formatter similar to the standard Formatter:

    StringBuffer buffer = new StringBuffer();
    Formatter formatter = new Formatter(buffer);
    formatter.format("hello %1$s, %2$s and %3$s", "me", "myself", "I");

The problem is that if you make a mistake in the format (e.g. you forget the $s) an exception will be thrown. Not really what I want when formatting an error message (since it s not in the regular path of the application and may not be tested).

I could of course define my own class, let s say accept a string like "hello $1, $2 and $3, do the replacement with ($1 -> %1$s) and invoke the toString() for all parameters but I m sure a better solution has been already developed.

Somewhere, in some jar...

Thanks for any useful pointer.

EDIT

I m looking for something that like the following:

String out = MySpecialFormatter.format("hello $1, $2 and $3", "me", "myself", "I");

if you have an error in your format (well, typos exist), it tries its best to fill in the bind variables, or return the original format string.

问题回答

Sometimes the answer is right there...

import static java.text.MessageFormat.format;

String out = format("hello {0}, {1} and {2}", "me", "myself", "2", "3");

And yes, Anon, apparently Sun thought that all the programmers where that lazy.

The problem is that if you make a mistake in the format (e.g. you forget the $s) an exception will be thrown. Not really what I want when formatting an error message (since it s not in the regular path of the application and may not be tested).

So you re basically asking "If I haven t tested my code and it s wrong, what can I use to hide that instead of having to deal with it?"

The solution is to properly test all your code. When you write it, preferably.

If you insist on covering up your bad code and pretending it s not there, you could do the following:

public class LazyProgrammersFormatter {
    private Formatter _f;

    public LazyProgrammersFormatter(Formatter f) {
        _f = f;
    }

    public bool format(/*can t be bothered looking up the varargs signature, you know what it is*/) {
        try {
            _f.format(/*etc.*/);
            return true;
        } catch (FormatException e) {
            return false;
        }
    }
}

Is String.format not suitable for what you are looking for? Java doc for string format.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签