如何计算运算符和操作数的字符串混合

问题描述:

当遇到操作符时发生错误。我知道运算符不能转换为
转换后的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

夫妇的问题:

  • 你只是在字符串要求输入但不处理它,因此会导致变量和引用的地方。
  • 定义一个变量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

如果你能做到这一点,那将是更容易使用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