String Formatter в GWT - PullRequest
       2

String Formatter в GWT

37 голосов
/ 27 июня 2010

Как мне отформатировать мою строку в GWT?

Я создал метод

  Formatter format = new Formatter();
    int matches = 0;
    Formatter formattedString = format.format("%d numbers(s, args) in correct position", matches);
    return formattedString.toString();

Но он жалуется, говоря:

Validating newly compiled units
   [ERROR] Errors in 'file:/C:/Documents%20and%20Settings/kkshetri/workspace/MasterMind/MasterMind/src/com/kunjan/MasterMind/client/MasterMind.java'
      [ERROR] Line 84: No source code is available for type java.util.Formatter; did you forget to inherit a required module?

Не включен ли Formatter

Ответы [ 12 ]

0 голосов
/ 05 мая 2011

другая очень очень простая замена для java.text.MessageFormat.format ():

public static String format(final String format, final Object... args) {
    StringBuilder sb = new StringBuilder();
    int cur = 0;
    int len = format.length();
    while (cur < len) {
        int fi = format.indexOf('{', cur);
        if (fi != -1) {
            sb.append(format.substring(cur, fi));
            int si = format.indexOf('}', fi);
            if (si != -1) {
                String nStr = format.substring(fi + 1, si);
                int i = Integer.parseInt(nStr);
                sb.append(args[i]);
                cur = si + 1;
            } else {
                sb.append(format.substring(fi));
                break;
            }
        } else {
            sb.append(format.substring(cur, len));
            break;
        }
    }
    return sb.toString();
}
0 голосов
/ 02 мая 2011

В качестве альтернативы вы можете использовать класс NumberFormat :

NumberFormat fmt = NumberFormat.getDecimalFormat();
double value = 12345.6789;
String formatted = fmt.format(value);
// Prints 1,2345.6789 in the default locale
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...