简体   繁体   English

反向波兰语符号和堆栈。 valueOf方法出错。 爪哇

[英]Reverse Polish Notation and stack. Error with valueOf method. Java

I have to calculate this expression (5+7)*(9-4)+12 , using the Stack and RPL (57+94-*12+) 我必须使用Stack和RPL (57 + 94- * 12 +)计算此表达式(5 + 7)*(9-4)+12

I loop through each element in the given array. 我遍历给定数组中的每个元素。 When it is a number, I push it to the stack. 当它是数字时,我将其推入堆栈。 When it is an operator, I pop two numbers from the stack, do the calculation, and push back the result. 当它是一个运算符时,我从堆栈中弹出两个数字,进行计算,然后推回结果。

So, here's my code. 所以,这是我的代码。 but there are errors in lines 33, 34, and 53 with valueOf method ( The method valueOf(String) in the type Integer is not applicable for the arguments (Object) ) 但是第33、34和53行的valueOf方法存在错误(Integer类型的valueValue(String)方法不适用于参数(Object)

Can You help me? 你能帮助我吗? Thanks! 谢谢!

package mainPackage;
import java.util.*;

public class Main {

    public static void main(String[] args) {

        String[] massive = {"5", "7", "+", "9", "4", "-", "*", "12", "+"};

        int result = calculate(massive);
        System.out.println(result);

    }

    public static int calculate(String[] mas) {
        Stack stack = new Stack();

        int sum = 0;
        String operators = "+-*/";

        for (String s : mas) {

            if ( !operators.contains(s)) {
                stack.push(s);
            }
            else {
                int a = Integer.valueOf(stack.pop());
                int b = Integer.valueOf(stack.pop());

                switch (s) {
                    case "+":
                        stack.push(String.valueOf(a + b));
                        break;
                    case "-":
                        stack.push(String.valueOf(b - a));
                        break;
                    case "*":
                        stack.push(String.valueOf(a * b));
                        break;
                    case "/":
                        stack.push(String.valueOf(b / a));
                        break;
                }
            }

        }
        sum = Integer.valueOf(stack.pop());

        return sum;
    }

}

The stack is storing Objects rather than Integers. 堆栈存储的是Objects而不是整数。 You have to explicitly cast them to Strings. 您必须将它们显式转换为字符串。 The compiler can't tell that they're Strings without you telling it, so it assumes you're trying to valueOf an Object , which is a superclass of String . 如果不告诉您,编译器就无法确定它们是String,因此它假定您正在尝试对Object进行valueOf的设置,该ObjectString的超类。

            int a = Integer.valueOf((String)stack.pop());
            int b = Integer.valueOf((String)stack.pop());

Alternatively, you can mention the type you intend to store in your declaration for the stack. 另外,您可以在堆栈的声明中提及要存储的类型。

    Stack<String> stack = new Stack<String>(); //Java 6

or 要么

    Stack<String> stack = new Stack<>(); //Java 7+

For additional reading on inheritance and casting, you can check out this tutorial . 有关继承和强制转换的更多信息,您可以查看本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM