2014-12-02 43 views
0

當遇到操作符時發生錯誤。我知道運算符不能轉換爲
轉換後的int或其他格式。我使用運算符通過讀取字節代碼來計算和傳遞給枚舉defined.But作爲我的字符串有運營商,所以我有prob在處理這些。請幫助我這一點。 ---- My Inputs is 1 + 2 ---- Expected Output-- 1 + 2 = 3 ---如何計算運算符和操作數的字符串混合

錯誤行---- b = Integer.parseInt(st.nextToken() );

--error在exceution期間----- 輸入系列 - 1 + 2

no of tokens:3 
yo 
1 
go 
1 
available 

byte info:10 
....... 
Exception in thread "main" java.lang.NumberFormatException: For input string: "+" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:484) 
    at java.lang.Integer.parseInt(Integer.java:527) 
    at Abc.main(Abc.java:42) 



I am not able to rectify it. Below is my code 

import java.io.*; 
import java.util.StringTokenizer; 

public class Abc{ 
public static void main(String[] args) throws Exception 
{ 
System.out.println("Hello World"); 
System.out.println("Enter the series"); 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
String s=br.readLine(); 
int a=0; 
int b=0; 
System.out.println(s); 
while ((br.readLine()) != null) 
{ 
StringTokenizer st=new StringTokenizer(s); 

while (st.hasMoreTokens()) 
{ 
int i=0; 
i=st.countTokens(); 
System.out.println("no of tokens:"+i); 
String token = st.nextToken(); 
System.out.println("yo"); 
System.out.println(token); 
System.out.println("go"); 


a=Integer.parseInt(token); 
System.out.println(a); 

if (st.hasMoreTokens()) // before consuming another token, make sure 
     { 
     System.out.println("available"); 
     byte b1=(byte)br.read(); 
     System.out.println("byte info:"+b1); 
         // there's one available 
         if (st.hasMoreTokens()){ 
       System.out.println("......."); 
     b = Integer.parseInt(st.nextToken()); 
     System.out.println("///////"); 

System.out.println(a); 
System.out.println("reached"); 
System.out.println(b); 
} 
if (b1==43) 
{ 
System.out.println("go"); 
int foo = Integer.parseInt(calculate(operator.ADDITION, a, b)); 
} 
else if (b1==45) 
{ 
int foo = Integer.parseInt(calculate(operator.SUBTRACTION, a, b)); 
} 
else if (b1==42) 
{ 
int foo = Integer.parseInt(calculate(operator.MULTIPLY, a, b)); 
} 
else if (b1==47) 
{ 
int foo = Integer.parseInt(calculate(operator.DIVIDE, a, b)); 
} 

} 
} 
} 
} 

public enum operator 
{ 
    ADDITION("+") { 
     public int apply(int x1, int x2) { 
      return x1 + x2; 
     } 
    }, 
    SUBTRACTION("-") { 
     public int apply(int x1, int x2) { 
      return x1 - x2; 
     } 
    }, 
MULTIPLY("*") { 
     public int apply(int x1, int x2) { 
      return x1 * x2; 
     } 
    }, 
    DIVIDE("/") { 
     public int apply(int x1, int x2) { 
      return x1/x2; 
     } 
    }; 

// You'd include other operators too... 
private final String text; 

    private operator(String text) { 
     this.text = text; 
    } 

    // Yes, enums *can* have abstract methods. This code compiles... 
    public abstract int apply(int x1, int x2); 

    public String toString() { 
     return text; 
    } 

} 

public static String calculate(operator op, int x1, int x2) 
{ 
    return String.valueOf(op.apply(x1, x2)); 
} 
} 
+0

歡迎來到SO。請正確格式化您的問題和您的代碼。 – m0skit0 2014-12-02 09:15:39

回答

0

夫婦的問題:

  • 你只是在字符串要求輸入但不處理它,因此會導致變量和引用的地方。
  • 定義一個變量String line;修改及更新您的while循環:

    ,而((行= br.readLine())!= NULL)

  • 你不需要這行byte b1=(byte)br.read();,因爲它只會有換行即輸入您輸入線時按鍵
  • while循環應該是:

    declare operand1, operand2, count as int 
    declare operator as char 
    while tokenizer has more tokens 
    do 
        optional validate String with token count as 3 with middle token as operator. 
        read token 
        if count == 0 then operand1 = int(token) 
        else if count == 1 then operator = char(token) 
        else operand2 = int(token) 
    done 
    
+0

我在下面使用string s變量,在stringtokenizer中傳遞它.how是操作符和操作數之間的diffrentiating(例如operator = char(token)),這是我確切的問題。 – 2014-12-02 09:52:38

+0

您正在接受像a + b這樣的參數。所以a是第一個標記,是操作數1,+是另一個操作符,b是最終的,並且是操作數2 – SMA 2014-12-02 09:56:22

0

如果你能做到這一點,那將是更容易使用Java的ScriptEngine類來評估用戶給定的字符串,像這樣:

ScriptEngineManager engine = new ScriptEngineManager(); 
ScriptEngine javaScript = engine.getEngineByName("JavaScript"); 

System.out.print("Please enter a mathematical operation: "); 
String op = new Scanner(System.in).nextLine(); 

try { 
    Object a = javaScript.eval(op); 
    System.out.println("Result: " + a.toString()); 
} catch (ScriptException ex) { 
    System.out.println("Error in the input!"); 
} 

我測試過它的正常工作。

+0

它的工作原理感謝user3189142 – 2014-12-03 07:24:54

+0

@MohitDarmwal沒問題。考慮將其標記爲已接受,以便其他人發現此問題時他們知道該怎麼做 – Shadow 2014-12-03 08:39:06

相關問題