2016-04-21 53 views
-1

我输入了一个代码将一个数字的输出打印成手机格式...如何将一个长整数转换为手机号码?

但它显示与此代码中使用的方法有关的错误...请帮助删除错误...如果它们都是可供选择的方法,然后请注明.. 这里的代码是

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); 
     } 
    } 
} 
+1

你得到的错误是什么? –

+1

为什么不尝试使用字符串作为电话号码类型? – Rugal

+3

电话号码不是整数。它具有领先的'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));