Java 如何将长整数分解为手机号码?
我必须输入一个代码,将数字输出打印成手机格式 但它显示了与此代码中使用的方法相关的错误。。。请帮助删除错误。。。如果它们是替代方法,请提及。。 代码在这里Java 如何将长整数分解为手机号码?,java,Java,我必须输入一个代码,将数字输出打印成手机格式 但它显示了与此代码中使用的方法相关的错误。。。请帮助删除错误。。。如果它们是替代方法,请提及。。 代码在这里 public class PrintAddressFormat { public static void main(String[] args) { String name,address; long phonenum; Scanner in = new Scanner(
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);
}
}
}
您可以使用MaskFormatter来完成此操作。请参阅下面的代码
String phoneMask= "###-###-####";
String phoneNumber= "1234567890";
MaskFormatter maskFormatter= new MaskFormatter(phoneMask);
maskFormatter.setValueContainsLiteralCharacters(false);
maskFormatter.valueToString(phoneNumber) ;
System.out.println(maskFormatter.valueToString(phoneNumber));
您得到的错误是什么?为什么不尝试使用字符串作为电话号码类型?电话号码不是整数。它有重要的前导
0
,有时还有+
和空格。