关于java中String.format()参数不适用问题

李小怪 2014-08-12 04:25:53
新人求助,对这句 String formata = String.format("%02d", num); 编译时出错。
Exception in thread "main" java.lang.Error: 无法解析的编译问题:
类型 String 中的方法 format(String, Object[])对于参数(String, int)不适用

这是怎么回事?jdk用1.5和1.8都不行
全部代码:
import java.text.Format;
import java.util.Scanner;
public class ChineseToCode
{

/**
* @param args
*/
public static void main(String[] args)
{
// TODO 自动生成的方法存根
Scanner scan1 = new Scanner(System.in);
System.out.println("请输入一个汉字:");
String text = scan1.nextLine();
if(text.length() > 2)
{
//JOptionPane.showMessageDialog(null, "不要输入过多的汉字。");
System.out.println("不要输入过多的汉字。");
return;
}
byte[] codeBit = text.getBytes();
if(codeBit.length < 2)
{
//JOptionPane.showMessageDialog(null, "要输入汉字。");
System.out.println( "要输入汉字。");
return;
}
codeBit[0] -= 160;
codeBit[1] -= 160;
String code = fromatNumber(codeBit[0]) + fromatNumber(codeBit[1]);

}

private static String fromatNumber(int num)
{
//int [] arr = {num};
String formata = String.format("%02d", num);
return formata;
}
}
...全文
1572 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Freed虾 2014-08-18
  • 打赏
  • 举报
回复
我是来看怎么解决问题的感谢楼上几位大大
xiaofeifeiloving 2014-08-13
  • 打赏
  • 举报
回复
类型 String 中的方法 format(String, Object[])对于参数(String, int)不适用这是肯定的呀,object[]数组和int型数据怎么能等同。。。
aquariusyimu 2014-08-12
  • 打赏
  • 举报
回复
format(String, Object[]) 很清楚,第二个参数是Object数组。 楼主传递的参数 1 类型不匹配,2 虽然java有自动装箱拆箱,但int不是Object。
小灯光环 2014-08-12
  • 打赏
  • 举报
回复 1
String formata = String.format("%02d",new Object[]{num}); 这样试试。
tony4geek 2014-08-12
  • 打赏
  • 举报
回复

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧