简体   繁体   English

我的前缀表达式求值器出现java.lang.OutOfMemory错误

[英]getting java.lang.OutOfMemory error with my prefix expression evaluator

Below i have the code for evaluating a prefix expression. 下面我有评估前缀表达式的代码。 I first read the expression into inputArray which allows me to read from right to left, then use evaluationArray to do the operations. 我首先将表达式读入inputArray,这使我可以从右到左阅读,然后使用评估数组进行操作。 I get out of memory error on this line: evaluationStack.push(number); 我在此行出现内存不足错误:EvaluationStack.push(number); I have no idea why this is happening. 我不知道为什么会这样。 any help would be greatly appreciated. 任何帮助将不胜感激。

import java.util.*; 导入java.util。*;

public class PrefixEvaluator 
{
    //evaluates a prefix expression
    public static int evaluate(String input)
    {
        int number, leftOperand, rightOperand, result;
        char operator;
        String inputToken = null;

        //create input stack
        Stack<String> inputStack = new Stack<String>();
        //create an integer stack
        Stack<Integer> evaluationStack = new Stack<Integer>();


        //create string tokenizer containing input string
        StringTokenizer tokenizer = new StringTokenizer(input);

        //read string into a stack
        while (tokenizer.hasMoreTokens())
        {
            inputToken = tokenizer.nextToken(); //get next token
                inputStack.push(inputToken);   //push token     
        }


        while (inputStack.isEmpty() != true)
        {      
           //System.out.println(inputStack.pop());

            if (isNumber(inputToken))
            {
                number = Integer.parseInt(inputToken);
                evaluationStack.push(number);
            }
            else                         //if token is operator
            {
                operator = inputToken.charAt(0);   //get operator
                rightOperand = evaluationStack.pop();
                leftOperand  = evaluationStack.pop();
                result = evaluation(operator, leftOperand, rightOperand);
                evaluationStack.push(result);
            }
        }
        return evaluationStack.pop();
    }

    //tests whether token is a number
    private static boolean isNumber(String token)
    {
        char first = token.charAt(0);
        if (Character.isDigit(first))
            return true;
        else
            return false;

    }

    //perform an operation on two operands
    private static int evaluation(char operator, int leftOperand, int rightOperand)
    {
        if (operator == '+')
            return leftOperand + rightOperand;
        else if (operator == '-')
            return leftOperand - rightOperand;
        else if (operator == '*')
            return leftOperand * rightOperand;
        else if (operator == '%')
            return leftOperand % rightOperand;
        else
            return leftOperand / rightOperand;

    }


    public static void main (String [] args)
    {
        evaluate("* 4 - 165 235");
    }

}

If you step through your code in your debugger you will see that this code is an infinite loop. 如果在调试器中逐步执行代码,您将看到此代码是一个无限循环。

    while (inputStack.isEmpty() != true)
    {      
       //System.out.println(inputStack.pop());

        if (isNumber(inputToken))
        {
            number = Integer.parseInt(inputToken);
            evaluationStack.push(number);

You keep testing inputToken and adding it to a stack but you never change it so it runs until you run out of memory. 您一直在测试inputToken并将其添加到堆栈中,但是您从不对其进行更改,因此它将一直运行到内存用完为止。

Perhaps you intended to have this at the start of the loop 也许您打算在循环开始时使用它

       inputToken = inputStack.pop();

If you also add 如果您还添加

System.out.println(evaluate("* 4 - 165 235"));

it prints 它打印

280

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

相关问题 java.lang.OutOfMemory错误-SD卡映像到ImageView中 - java.lang.OutOfMemory Error - SD Card Image into ImageView android上的java.lang.outofmemory错误(Web服务) - java.lang.outofmemory error (web services) on android 从数据库中获取记录时出现java.lang.OutOfMemory错误 - java.lang.OutOfMemory error when fetching records from Database PageOutputStream导致java.lang.OutOfMemory - PageOutputStream causing java.lang.OutOfMemory 如何解决Java错误“ pool-1-thread-xxxx” java.lang.OutOfMemory - How to solve Java error “pool-1-thread-xxxx” java.lang.OutOfMemory 对来自摄像头的视频进行图像处理操作时出现java.lang.OutOfMemory错误 - java.lang.OutOfMemory error when doing image processing operation on video from webcam 尝试发送HTTP请求时,Tomcat与java.lang.OutOfMemory崩溃时出错 - Tomcat crashes with java.lang.OutOfMemory Error while trying to send an HTTP request 解决java.lang.outofmemory错误,导致一半文件上载到服务器 - Resolving java.lang.outofmemory error causing half of the file to be uploaded to server 在循环内使用子字符串函数时出现Java.lang.OutOfMemory错误 - Java.lang.OutOfMemory error while using substring function inside a loop SWT应用程序:Java堆空间:java.lang.OutOfMemory - SWT application : Java heap space: java.lang.OutOfMemory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM