System.out.println(String.format("%1$,09d", -3123)); 我的Eclipse老是报The method format(String, Object[]) in the type String is not applicable for the arguments (String, int)这个呢!?不知道市哪里用错了,请教各位高
/** * 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 * the <a href="http://java.sun.com/docs/books/vmspec/">Java * Virtual Machine Specification</a>. 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(); }
public Formatter format(String format, Object ... args) { return format(l, format, args); } public Formatter format(Locale l, String format, Object ... args) { ensureOpen(); // index of last argument referenced int last = -1; // last ordinary index int lasto = -1; FormatString[] fsa = parse(format); for (int i = 0; i < fsa.length; i++) { FormatString fs = fsa[i]; int index = fs.index(); try { switch (index) { case -2: // fixed string, "%n", or "%%" fs.print(null, l); break; case -1: // relative index if (last < 0 || (args != null && last > args.length - 1)) throw new MissingFormatArgumentException(fs.toString()); fs.print((args == null ? null : args[last]), l); break; case 0: // ordinary index lasto++; last = lasto; if (args != null && lasto > args.length - 1) throw new MissingFormatArgumentException(fs.toString()); fs.print((args == null ? null : args[lasto]), l); break; default: // explicit index last = index - 1; if (args != null && last > args.length - 1) throw new MissingFormatArgumentException(fs.toString()); fs.print((args == null ? null : args[last]), l); break; } } catch (IOException x) { lastException = x; } } return this; }
楼主【herowhpa】截止到2008-08-01 21:31:09的历史汇总数据(不包括此帖):
发帖的总数量:0 发帖的总分数:0 每贴平均分数:0
回帖的总数量:7 得分贴总数量:2 回帖的得分率:28%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:---------------------结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=herowhpa
format(String, Object[])
接受的参数是一个字符串对象和一个Object对象,而INT是基本类型,不是对象
是要搞清楚。不过可以定义一个INTEGE对象
public static void main (String args[]){
String string = "1000";
String str = String.format("%1$s:%2$s", string, string+"1000");
System.out.print(str);
}
}
1000:10001000
* 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
* the <a href="http://java.sun.com/docs/books/vmspec/">Java
* Virtual Machine Specification</a>. 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();
}
return format(l, format, args);
}
public Formatter format(Locale l, String format, Object ... args) {
ensureOpen(); // index of last argument referenced
int last = -1;
// last ordinary index
int lasto = -1; FormatString[] fsa = parse(format);
for (int i = 0; i < fsa.length; i++) {
FormatString fs = fsa[i];
int index = fs.index();
try {
switch (index) {
case -2: // fixed string, "%n", or "%%"
fs.print(null, l);
break;
case -1: // relative index
if (last < 0 || (args != null && last > args.length - 1))
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[last]), l);
break;
case 0: // ordinary index
lasto++;
last = lasto;
if (args != null && lasto > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[lasto]), l);
break;
default: // explicit index
last = index - 1;
if (args != null && last > args.length - 1)
throw new MissingFormatArgumentException(fs.toString());
fs.print((args == null ? null : args[last]), l);
break;
}
} catch (IOException x) {
lastException = x;
}
}
return this;
}