2010-03-21 69 views
1

好吧,我迷路了。我需要弄清楚如何驗證一個整數,但出於某種愚蠢的原因,我不能使用Try-Catch方法。我知道這是最簡單的方法,因此互聯網上的所有解決方案都在使用它。驗證整數或字符串沒有try-catch

我正在用Java編寫。

交易是這樣的,我需要有人把一個數字ID和字符串名稱。如果兩個輸入中的任何一個無效,我必須告訴他們他們犯了一個錯誤。

有人可以幫助我嗎?

+0

不指定什麼構成一個有效的字符串輸入。從技術上講,所有輸入都是字符串,所以什麼是無效的 – cletus 2010-03-21 02:48:39

+0

哦,是的,我想我不必檢查那個。謝謝哈哈,只是整數驗證! – Phil 2010-03-21 02:56:37

+0

那麼一個只包含空格的字符串呢?這可能是你想要認爲無效的事情。 – MatrixFrog 2010-03-21 03:18:00

回答

5

如果我理解正確的話,你正在閱讀從標準輸入字符串形式的整數或字符串,並且你想驗證該整數實際上是一個整數。也許你遇到的麻煩是Integer.parseInt()它可以用於將String轉換爲一個整數拋出NumberFormatException。這聽起來像你的任務禁止使用異常處理(我是否理解正確),所以你不能使用這個內置函數,並且必須自己實現它。

好的。所以,因爲這是家庭作業,我不會給你完整的答案,但這裏是僞代碼:

 
let result = 0 // accumulator for our result 
let radix = 10 // base 10 number 
let isneg = false // not negative as far as we are aware 

strip leading/trailing whitespace for the input string 

if the input begins with '+': 
    remove the '+' 
otherwise, if the input begins with '-': 
    remove the '-' 
    set isneg to true 

for each character in the input string: 
    if the character is not a digit: 
     indicate failure 
    otherwise: 
     multiply result by the radix 
     add the character, converted to a digit, to the result 

if isneg: 
    negate the result 

report the result 

這裏的關鍵是,每個數字的基數倍,比數字更顯著直接將其正確的,所以如果我們總是乘以基數,當我們從左到右掃描字符串時,每個數字都有其適當的意義。現在,如果我錯了,你可以實際使用的try-catch但根本還沒有想出如何:

 
int result = 0; 
boolean done = false; 
while (!done){ 
    String str = // read the input 
    try{ 
     result = Integer.parseInt(str); 
     done = true; 
    }catch(NumberFormatException the_input_string_isnt_an_integer){ 
     // ask the user to try again 
    } 
} 
+0

或者你可以使用正則表達式,但這可能是這個任務矯枉過正。當然,你知道他們對有問題的人說什麼,並認爲「我知道 - 我會用正則表達式!」 – MatrixFrog 2010-03-21 03:17:18

+0

哇。我實際上正在學習一門非常基礎的Java課程,當時這真的幫助了哈哈! – Phil 2010-03-21 03:49:25

0

看看Scanner中的hasNext方法。請注意,每種類型都有方法(例如hasNextBigDecimal),而不僅僅是hasNext。

編輯:不,這不會拋出無效輸入,只有當掃描器過早關閉。

+0

這是我的想法,但有input.hasNext()仍然會拋出異常..不是嗎? – Phil 2010-03-21 02:56:03

3

不能完全確定是否正在驗證的String應檢查內容是否僅包含數字,或可在int的有效範圍內實際代表,來解決這個問題的一種方法是迭代的字符String並檢查字符是否僅由數字組成。

因爲這似乎是功課,這裏是僞代碼的一點點:

define isANumber(String input): 
    for (character in input): 
    if (character is not a number): 
     return false 

    return true 
0
Pattern p = Pattern.compile("\\d{1,}"); 
Matcher m = p.matcher(inputStr); 
boolean isNumber = false; 

if (m.find()) { 
    isNumber = true; 
} 
+0

這是正確的想法,但你需要迎合一個可選的標誌(容易)和數字的可能性太大(凌亂,除非你限制域)。 – 2010-03-21 03:42:26

0
public static boolean IsNumeric(String s) 
{ 
    if (s == null) 
     return false; 
    if (s.length() == 0) 
     return false; 
    for (int i = 0; i < s.length(); i++) 
    { 
     if (!Character.isDigit(s.charAt(i))) 
      return false; 
    } 
    return true; 
}  
1

您可以使用java.util.ScannerhasNextInt(),以驗證是否能String被轉換爲int而不會引發異常。

作爲一個額外的功能,它可以跳過空格,並容忍額外的垃圾(你可以檢查)。

String[] ss = { 
    "1000000000000000000", 
    " -300 ", 
    "3.14159", 
    "a dozen", 
    "99 bottles of beer", 
}; 
for (String s : ss) { 
    System.out.println(new Scanner(s).hasNextInt()); 
} // prints false, true, false, false, true 

參見:How do I keep a scanner from throwing exceptions when the wrong type is entered?

2

我使用Java 1。6,這對我的作品:

if (yourStringVariable.trim().matches("^\\d*$")) 

那麼你已經有了一個正整數