2015-03-19 146 views
0

我想確定一個數字是否是像這樣的表達式中的整數。 (10 * 2)+5。如果表達式是(10 * 2)+5.24,我希望程序說那個表達式中有一個非整數。當前代碼:如何在表達式中找出數字是否爲整數?

public static boolean isInt(String expr) { 
    for (int i = 0; i<expr.length(); i++){ 
     if (expr. charAt(i) != (int)i){ 
     return false; 
     } 
    } 
    return true; 
} 

問題是除了數字之外還有更多的字符。所以我希望它只檢查數字並忽略所有其他符號,以確定字符串中的每個數字是否爲整數。

+0

如果(表達式。 contains(「。」)) – 2015-03-19 00:36:59

+0

表達式是否只有數字,其他字符如()+ *,對不對?或錯誤的輸入(字母)也? – RahulArackal 2015-03-19 00:45:43

+0

要評估包括數學運算的數值表達式,請嘗試使用ScriptEngine類,如綠色箭頭所示,請參閱http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java。要使用Java來評估它,會涉及到編寫一個將它執行到外部文件的類,然後編譯並將其jar作爲外部可執行文件運行。 – 2015-03-19 01:07:22

回答

0

我會開始標記爲「單詞」,拆分*/+ - ()^和空格。如果我明白你在做什麼,結果應該只是數字。

要使用番石榴分割: 可重複令牌= Splitter.on(CharMatcher.anyOf(「 - /*+(){")).split(myExpression);

然後你就可以掃描 「字」 尋找一個小數點由@Scary袋熊的建議

下面是一個完整的版本:

public void test() { 

    char[] values = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 
    List<Character> ints = Chars.asList(values); 

    String str = "the values are 4 and 4.03 tons"; 
    Iterable<String> tokens = Splitter.on(CharMatcher.anyOf("- /*+()^")).split(str); 

    for (String token : tokens) { 
     char[] chars = token.toCharArray(); 
     for (char c : chars) { 
      if (!ints.contains(c)) { 
       System.out.println(token + " is not an int"); 
       break; 
      } 
     } 
    } 
} 

生產:

the is not an int 
values is not an int 
are is not an int 
and is not an int 
4.03 is not an int 
tons is not an int 
0

見如果這可以滿足您的需求

String str = "the values are 4 and 4.03 tons"; 
Pattern p = Pattern.compile("\\d+[.]?\\d*"); 
Matcher m = p.matcher(str); 
while (m.find()) { 
    String ss = m.group(); 
    int temp_i; 
    try { 
     temp_i = Integer.parseInt(ss); 
     System.out.println(temp_i); 
    } catch (NumberFormatException nfe) { 
     System.out.println(String.format("%s not integer", ss)); 
    }//end try 
}//end while 

或者,以滿足浮動,如3E-2,使用

Pattern p = Pattern.compile("\\d+[.eE]?[-]?\\d*"); 

希望它能幫助。

0

我們可以用reg表達式來實現它。

String regExp = "([0-9]*\\.[0-9]*)"; 

然後計算找到的每個相等的匹配數量:

所以, 完整的代碼是在這裏:

import java.util.regex.Pattern; 
import java.util.regex.Matcher; 
public class RegExpDemo { 
    public static void main(String[] args) { 
     String regExp = "([0-9]*\\.[0-9]*)"; 
     String input = "-1 + 5 - (10 * 2) + 5.24/(3.146 * 22/100)"; 
     try { 
      Pattern pattern = Pattern.compile(regExp); 
      Matcher matcher = pattern.matcher(input); 
      int count = 0; 
      while(matcher.find()) { 
       System.out.println("Result "+matcher.group()); 
       count++; 
       //System.out.println("Count "+matcher.groupCount());  
      } 
      System.out.println("Total Count "+count);   
     } catch(Exception exp) { 
      System.out.println(exp); 
     } 
    } 
} 
O/p 
-------------- 
Result 5.24 
Result 3.146 
Total Count 2 

我希望輸入只包含有效的數字格式和其他任何字符不使用。即; 1,26而不是1.26。如果有任何其他字符,你可能需要在模式取決於

此外,如果你需要統計值像1..26然後使用量詞+與修改。(點)

 String regExp = "([0-9]*\\.+[0-9]*)"; 
     String input = "-1 + 5 - (10 * 2) + 5.24/(3.146 * 22/100) - 1..26"; 
相關問題