2016-03-02 68 views
0

我想驗證用戶輸入的電話號碼,我需要做的第一件事是檢查,以確保輸入字符串的長度是10哪個是這是您在代碼中看到的問題。java驗證字符串包含數字和搜索,以確保沒有字符

現在是問題出現的地方,我還需要檢查以確保沒有輸入字符,以及有關如何在不使用數組的情況下執行此操作的任何想法。我想使用Character.isLetter();但無論我如何實施它,它都不是正確的。

import java.util.Scanner; 
public class Example 
{ 
    public static void main(String[] args) 
    { 
     boolean isNumber = false; 
     String phoneNum = ""; 
     Scanner input = new Scanner(System.in); 

     while(!isNumber) 
     { 
      System.out.print("\nEnter your 10 digit phone number (ex: 2123345657): ");   
      phoneNum = input.nextLine(); 

      /* Yes I could use nextInt(); etc.. but that would defeat the 
       purpose of this exercise */ 

       if(phoneNum.length() != 10) 
       { 
       System.out.println("\n\nBad input"); 
       } 
       else 
        isNumber = true; 
     } 

     System.out.println("\n\nPhone number is: " + phoneNum); 


    } 

} 

我在找這樣的事情。

while(!isRight) 
    { 
      System.out.print("\n input number: "); 
      phoneNum = input.nextLine(); 

      while(i < phoneNum.length()) 
      { 
       if(phoneNum.length() != 10) 
       { 
        System.out.println("not enough numbers"); 
       } 


       else if (Character.isLetter(phoneNum.charAt(i)) == true) 
       { 

        System.out.println("Sorry you can't use any thing but numbers"); 
       } 

       else 
        isRight = true; 
       i++; 
      } 
    } 

回答

0

我想後,我想出瞭解決方案,有幾個原因,但主要是這樣,如果這樣一些一個是有問題,他們會故意避開數組,正則表達式等。他們可以有一個良好的例!

import java.util.Scanner; 
public class Example 
{ 
    public static void main(String[] args) 
    { 
     boolean isNumber = false; 
     String phoneNum = ""; 
     Scanner input = new Scanner(System.in); 

     while(!isNumber) 
     { 
      isNumber = true; 
      System.out.print("\nEnter your 10 digit phone number (ex: 4142317534): "); 
      phoneNum = input.nextLine(); 

      for(int i = 0; i < phoneNum.length(); i++) 
      { 
       if(!Character.isDigit(phoneNum.charAt(i))) 
       { 
        isNumber = false; 
       } 
      } 


      if(phoneNum.length() != 10) 
      { 
       isNumber = false; 
      } 


      if(isNumber == false) 
      { 
       System.out.println("\n\nInvalid format - please reenter"); 
      } 
     } 

     System.out.println("\n\nPhone Number: " + phoneNum); 

    } 


} 

是的,它是一個很大的代碼是可以凝結,但它是學習和了解如何工作更重要,爲什麼他們的工作,他們的方式嘗試是有效的才做,效率自帶學習工作越多,你做得越好,你得到的垃圾就越多!

1

使用

"^[0-9]{10}" 

一個正則表達式,或者如果它開始與非零

"^[1-9][0-9]{9}" 

if(!phoneNum.matches("^[0-9]{10}")) { 
    // ok 
} 

如果你想使用isLetter

然後嘗試

boolean allNum = true; 
for (char ch : phoneNum.toCharArray()) { 
    if (Character.isLetter (ch)) { 
     allNum = false; 
     break; 
    } 
} 
+0

爲什麼不''[0-9] {10}'? –

+0

@AndyTurner因爲我的咖啡機今天早上壞了 –

+0

是的,我可以這樣做,我曾經希望使用Character.isLetter()方法的一些東西。我似乎無法正確實施。 – Afflicted

0

您可以用String正則表達式匹配檢查字符串是否是一個數字或不:

boolean numeric = phoneNum.matches("\\d+"); 

作爲替代方案,如果您有項目導入阿帕奇公共圖書館,你可以使用StringUtilsisNumeric方法類,如:

StringUtils.isNumeric(phoneNum);

1

你可以不用正則表達式:

boolean validate(String str) { 
    if (str.length() != 10) { 
    return false; 
    } 
    for (int i = 0; i < str.length(); ++i) { 
    if (!Character.isDigit(str.charAt(i)) { 
     return false; 
    } 
    } 
    return true; 
} 
0

您可以使用此: /*檢查是數字*/

StringUtils.isNumeric(yourNumber); 

或正則表達式來避免所有字符diferente數字。

`{\\d+}` 
相關問題