2016-04-21 51 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));