2016-02-13 38 views
-2

我有一個存儲問題的String類型的名爲questions array的數組。 我想將questionArray[0]轉換爲int。 我已經使用下面的語句來做到這一點。如何將包含數學表達式的字符串轉換爲整數?

int aa = Integer.parseInt(questionArray[0]); 

但是,當我實現這個說法,當我運行該應用程序我得到一個錯誤說:invalid int: "10-4"。 請注意,10-4可以是任何隨機算術表達式,因爲它是一個隨機問題遊戲。例如:9+110/5

+0

如果我理解正確questionArray [0]等於「10-4」,這是一個字符串不是數字的字符串, '字符。如果我沒有記錯,ParseInt只能處理數字,不能表達。你將不得不寫一個函數,從字符串中提取第一個和第二個數字,然後按照操作符在 –

+0

之間進行數學運算。我刪除了我的答案,現在我正在研究新的答案。保持緊張 –

+0

爲什麼這個問題有這麼低的分數?它遵循所有的規則來提出一個好問題,而且這個問題很明顯是什麼......?夥計們,不要只是投票,至少留下一個評論..! –

回答

0

這比解析String有點複雜,因爲它有一個算術符號,它可以是任何東西。所以,讓我們去在它一步一步:

//Lets say you have a string like this 
questionArray[0] = "10+4"; 
//Lets find the Arithmetic sign first in the String 
String sign = null; 

Pattern regex = Pattern.compile("[+*/]|(?<=\\s)-"); 
Matcher matcher = regex.matcher(questionArray[0]); 
    while(matcher.find()) { 
     sign = matcher.group().trim(); //Store that Arithmetic sign here.  
    } 

String [] parts = questionArray[0].split("[+*/]|(?<=\\s)-"); //Now break up the string using this regex. 
               //This regex will find whatever Arithmetic sign 
               //there is inside this String and store the result 
               //in parts array. 

int firstNumber = Integer.parseInt(parts[0]); //The first number in the String 
int secondNumber = Integer.parseInt(parts[1]);//The second number in the String 

//a simple if-else statements that will help us find the final answer. 
int result = 0; 
if (sign.equals("+")){ 
    result = firstNumber + secondNumber; 
} else if (sign.equals("-")) { 
    result = firstNumber - secondNumber; 
} else if(sign.equals("*")) {  
    result = firstNumber * secondNumber; 
} else if (sign.equals("/")){ 
    result = firstNumber/secondNumber; 
} else { 
    System.out.println("unknown operation"); 
} 

System.out.println(result); 
+0

OP確實這麼說。 – Hackerdarshi

+0

@FarazDurrani如果它的/,*或+怎麼辦? – Harsh

+0

@法拉茲杜拉尼不需要粗魯! +你有4個if條件,這些條件是彼此獨佔的。但所有4個都被執行。也許你的代碼會受益於一些'else if's。或者甚至可能是一個「開關盒」? –

3

簡單的回答是,「10-4」不是一個簡單的整數,這是一個計算,所以它解析爲int會沒有結果..

你必須分析你的字符串..

int aa = evaluteQuestion(questionArray[0]); 

和實際的奇蹟發生在這裏:

public static int evaluteQuestion(String question){ 
    Scanner sc = new Scanner(question); 

    // get the next number from the scanner 
    int firstValue = Integer.parseInt(sc.findInLine("[0-9]*")); 

    // get everything which follows and is not a number (might contain white spaces) 
    String operator = sc.findInLine("[^0-9]*").trim(); 
    int secondValue = Integer.parseInt(sc.findInLine("[0-9]*")); 
    switch (operator){ 
     case "+": 
      return firstValue + secondValue; 
     case "-": 
      return firstValue - secondValue; 
     case "/": 
      return firstValue/secondValue; 
     case "*": 
      return firstValue * secondValue; 
     case "%": 
      return firstValue % secondValue; 
     // todo: add additional operators as needed.. 
     default: 
      throw new RuntimeException("unknown operator: "+operator); 
    } 
} 

如果你有莫在您的表達式中重新部分,您可能想要將上面的代碼放入循環中。但請注意操作順序。如果你想爲任何表達式實現合適的解析器,事情可能會變得有點多毛

+1

你需要使'int result = 0;'。否則,編譯器將顯示錯誤。 – Hackerdarshi

+0

你去了,修好了;) –

+0

@Hackerdarshi不,這不是必須的,因爲程序在初始化之前不會達到'return result'。該修補程序仍然值得,因爲它現在是更好的代碼,恕我直言。 – Tom

相關問題