关于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;
}
}