我输入了一个代码,用于将数字的输出打印成手机格式......
但它显示与此代码中使用方法相关的错误...请帮助删除错误...如果它们是替代方法,请提及.. 这里的代码是
public class PrintAddressFormat
{
public static void main(String[] args)
{
String name,address;
long phonenum;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Name : ");
name=in.nextLine();
System.out.print("Enter the Address : \n");
address=in.nextLine();
System.out.print("Enter the Phone number : \n");
phonenum=in.nextLong();
System.out.println("**********************************************");
System.out.println("Name : "+name);
System.out.println("Address : "+address);
System.out.println("Phone Number : "+ phoneFormat(phonenum));
System.out.println("**********************************************");
void phoneFormat(long temp)
{
long rem1=temp%10000;
temp=temp/10000;
long rem2=temp%100;
temp=temp/100;
System.out.println(temp+"-"+rem2+"-"+rem1);
}
}
}
答案 0 :(得分:2)
您可以使用MaskFormatter执行此操作。见下面的代码
String phoneMask= "###-###-####";
String phoneNumber= "1234567890";
MaskFormatter maskFormatter= new MaskFormatter(phoneMask);
maskFormatter.setValueContainsLiteralCharacters(false);
maskFormatter.valueToString(phoneNumber) ;
System.out.println(maskFormatter.valueToString(phoneNumber));