2017-09-16 111 views
0

我必须使用堆栈评估前缀表达式,我这样做了,但我不明白为什么代码无法正常工作,它在编译代码时标记了2个错误,它们是:使用堆栈评估前缀表达式

异常在线程 “主要” java.lang.ClassCastException:java.lang.String中不能在evaluationprefix.EvaluationPreFix.EvaluationPrefix(EvaluationPreFix.java:56) 被强制转换为java.lang.Integer中 在evaluationprefix.EvaluationPreFix。 main(EvaluationPreFix.java:25)

public class EvaluationPreFix { 

public static void main(String[] args) { 
    Stack st = new Stack(); 
    Scanner sc = new Scanner(System.in); 

    System.out.println("enter the size of expression"); 
    int t = sc.nextInt(); 
    sc.nextLine(); 
    for (int i = 0; i < t; i++) { 
     System.out.println("enter an element"); 
     String element = sc.nextLine(); 
     st.push(element); 
    } 

    int r = EvaluationPrefix(st); //marks an Error here 
    System.out.println("Result: " + r); 

} 

public static int EvaluationPrefix(Stack st) { 
    Stack st2 = new Stack(); 

    while (!st.isEmpty()) { 
     Object e = st.pop(); 
     if (e.equals('+')) { 
      st2.push((Integer) st2.pop() + (Integer) st2.pop()); 
     } else if (e.equals('-')) { 
      st2.push((Integer) st2.pop() - (Integer) st2.pop()); 
     } else if (e.equals('*')) { 
      st2.push((Integer) st2.pop() * (Integer) st2.pop()); 
     } else if (e.equals('/')) { 
      st2.push((Integer) st2.pop()/(Integer) st2.pop()); 
     } else { 
      st2.push(e); 
     } 
    } 
    return (Integer) st2.pop();//marks an error here 
} 

} 

回答

0

所做的更改:

  • 方法,改变了堆,ST,到String类型。
  • EvaluationPrefix方法,
    • 改变的参数堆栈String类型。
    • 更改了堆栈,st2,到Integer类型。
    • 将算术运算符equals更改为String

在这里你去,

public class EvaluationPreFix { 

    public static void main(String[] args) { 
     //1. parameterized with String 
     Stack<String> st = new Stack(); 
     Scanner sc = new Scanner(System.in); 

     System.out.println("enter the size of expression"); 
     int t = sc.nextInt(); 
     sc.nextLine(); 
     for (int i = 0; i < t; i++) { 
      System.out.println("enter an element"); 
      String element = sc.nextLine(); 
      st.push(element); 
     } 

     int r = EvaluationPrefix(st); //marks an Error here 
     System.out.println("Result: " + r); 

    } 

    //2. parameterized with String 
    public static int EvaluationPrefix(Stack<String> st) { 
     //3. parameterized with Integer 
     Stack<Integer> st2 = new Stack(); 

     while (!st.isEmpty()) { 
      String e = st.pop(); 
      //4. arithmetic sign comparison to string instead 
      //of character 
      if (e.equals("+")) { 
       st2.push(st2.pop() + st2.pop()); 
      } else if (e.equals("-")) { 
       st2.push(st2.pop() - st2.pop()); 
      } else if (e.equals("*")) { 
       st2.push(st2.pop() * st2.pop()); 
      } else if (e.equals("/")) { 
       st2.push(st2.pop()/st2.pop()); 
      } else { 
       st2.push(Integer.valueOf(e)); 
      } 
     } 

     return st2.pop(); 
    } 

} 
+0

我想无论你使用'st.push'它应该是'st2.push'(当检查操作员时) –

+0

只有最后一个去'st2'。试试看,例如,2 3 +会给你5的结果。你必须推回到同一个堆栈。 –

+0

但它是前缀,所以它应该是+ 2 3 –

0

假设我们正在谈论java.util.stack - 这只是一个存储您推入它的集合,并且您正在使用它作为原始类型

Stack st = new Stack(); 

这意味着您可以将任何类型的对象推入此堆栈。看起来你只想存储Integers - 通过使用泛型告诉编译器。

Stack<Integer> st = new Stack<>(); 

这会告诉你,这个问题是无论你尝试e转换为整数by casting, because in your case, the values ofËare the字符串s you pused into ST in的main()`。

你也应该

​​

和方法声明取代st声明中main

public static int EvaluationPrefix(Stack<String> st) 

突出问题。

如果您有String并且想将其转换为Integer,则需要对其进行解析,例如使用Integer.parseInt。但是您需要知道,如果String不是数字,则此方法将抛出NumberFormatException。您将必须处理此异常,例如通过捕获它并打印出有用的错误消息。

+0

权,所以现在的问题就在这里'st2.push(E);''因为是e''String'和'st2'被只适用于整数,所以我该如何解决它? –

+0

我不能铸造 –