为什么String不被识别为Object

时间:2014-02-17 14:25:23

标签: java map

我正在使用GNU Make编译我的Java项目,它不支持泛型。

这是我的小代码导致错误:

 for (int i = 0; i < text.length(); i++) {
  if (text.charAt(i) > 127) {
      buffer.append("\"");
      String charValue = asciiToNascMap.get(String.valueOf((int)text.charAt(i)));
      buffer.append(String.format(";CHR$(%d);\"", charValue));
     } else {
         buffer.append(text.charAt(i));
       }
   }

   private static Map<String, String>   asciiToNascMap = new HashMap<String, String>();

static {
  asciiToNascMap.put("232", "125");//è
  asciiToNascMap.put("233", "123");//é
  asciiToNascMap.put("224", "64");//à
  asciiToNascMap.put("231", "92");//ç
  asciiToNascMap.put("199", "180");//Ç
  asciiToNascMap.put("234", "193");//ê
}

我收到了这个错误:

 Printer.java:319: error:Cannot find method "java/lang/String.format(java.lang.String, java.lang.String)"

这是String.format方法的签名:

 /**
 * Returns a formatted string using the specified format string and
 * arguments.
 *
 * <p> The locale always used is the one returned by {@link
 * java.util.Locale#getDefault() Locale.getDefault()}.
 *
 * @param  format
 *         A <a href="../util/Formatter.html#syntax">format string</a>
 *
 * @param  args
 *         Arguments referenced by the format specifiers in the format
 *         string.  If there are more arguments than format specifiers, the
 *         extra arguments are ignored.  The number of arguments is
 *         variable and may be zero.  The maximum number of arguments is
 *         limited by the maximum dimension of a Java array as defined by
 *         <cite>The Java&trade; Virtual Machine Specification</cite>.
 *         The behaviour on a
 *         <tt>null</tt> argument depends on the <a
 *         href="../util/Formatter.html#syntax">conversion</a>.
 *
 * @throws  IllegalFormatException
 *          If a format string contains an illegal syntax, a format
 *          specifier that is incompatible with the given arguments,
 *          insufficient arguments given the format string, or other
 *          illegal conditions.  For specification of all possible
 *          formatting errors, see the <a
 *          href="../util/Formatter.html#detail">Details</a> section of the
 *          formatter class specification.
 *
 * @throws  NullPointerException
 *          If the <tt>format</tt> is <tt>null</tt>
 *
 * @return  A formatted string
 *
 * @see  java.util.Formatter
 * @since  1.5
 */
public static String format(String format, Object ... args) {
    return new Formatter().format(format, args).toString();
}

3 个答案:

答案 0 :(得分:5)

您尝试使用的方法在Java 1.4中不可用。查看已发布评论中的@since标记。它说@since 1.5意味着它在Java 1.5中被引入。

答案 1 :(得分:3)

您不能在Java 1.4中使用String.format

尝试:

buffer.append(";CHR$(").append(Integer.valueOf(charValue)).append(");\"");

答案 2 :(得分:2)

Java 1.4中没有

String.format。您在@since标记中发布的文字中指出了这一点。直到Java 5才添加varargs支持,这就是为什么它可能直到那时才被添加。但是,人们创建了C printf-style个库。

请参阅以下链接:

http://www.sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/introduction.htm http://www.cs.ubc.ca/~lloyd/java/doc/cformat.html