为什么我的程序找不到类Stack?

时间:2014-05-04 04:41:46

标签: java stack

对于我和我的合作伙伴的项目,我们正在尝试用Java构建计算器程序。你能告诉我有什么问题吗?

public class InfixtoPostFixParens
{
    // instance variables - replace the example below with your own
    //data fields
    /**The operator Stack */
    private Stack<Character> operatorStack;
    /** THe operator */
    private static final String OPERATORS = "+-*/";
    /** The precedence of the operators mathces the order in Operators */
    private static final int[] PRECENDENCE = {0,1,1,2,2,2};
    /** Postfix string */
    private StringBuilder postfix;

    private void processOperator(char op){
        if(operatorStack.empty() || op == '('){
            operatorStack.push(op);
        }else{
            //peek the operator stack
            //and let topOp be the top operator
            char topOp = operatorStack.peek();
            if(precedence(op) > precedence(topOp)){
                operatorStack.push(op);
            }else{
                //pop a;; stacked p[eratprs wotj equla or higher precedence than op
                while(!operatorStack.empty() && precedence(op) <= precedence(topOp)){
                    operatorStack.pop();
                    if(topOp == '('){
                        //matching ( popped - exit loop
                        break;
                    }
                    postfix.append(topOp);
                    postfix.append(' ');
                    if(!operatorStack.empty()){
                        //reset topOp
                        topOp = operatorStack.peek();
                    }
                }
                //assert operator is empty ot current operator precedence > top of stack operator precedence
                if(op != ')')
                {
                    operatorStack.push(op);
                }
            }
        }
    }

    public String convert(String infix){
        operatorStack = new Stack<Character>();
        postfix = new Stringbuilder();

        try{
            //process each token in the infix epression
            String nextToken;
            Scanner scan = new Scanner(infix);
            while((nextToken = scan.findLine("[\\p{L}\\p{N}]+|[-+/\\*()]")) != null){
                    char firstChar = nextToken.charAt(0);
                    //is it an opperand
                    if(Character.isJavaIdentifierStart(firstChar)){
                        postfix.append(nextToken);
                        postfix(' ');
                    }// is it a operator
                    else if(isOperator(firstChar)){
                        processOperator(firstChar);
                    }else{
                        //throw new error
                    }
                }

                while(!operatorStack.empty()){
                    char op = operatorStack.pop();
                    if(op == '('){
                        //throw error
                    }
                    postfix.append(op);
                    postfix.append(' ');
                    return postfix.toString();
                }
        }catch(EmptyStackException ex){
            //throw error
        }
    }
}

当我编译它时,我收到错误说&#34;它找不到符号类Stack。&#34;

1 个答案:

答案 0 :(得分:3)

Stack是可用于Java程序的大量类之一。但是,绝大多数Java程序并不需要使用这些类中的绝大多数,因此将所有这些类都包含在每个程序中都是毫无意义的(而且,无论如何都是不可能的)。相反,我们将所有这些类存储在libraries中,并且每次编写自己的类时,都需要import从这些库中获取所需的特定类。

您可以使用import语句来执行此操作,这些语句应放在类文件的顶部,位于包声明下方和类声明之上:

package yourPackageName;

import java.util.Stack;
//other imports 

class yourClass { 
//constructor, code, etc... 
}

请注意,import语句的语法是:libraryName.libraryName.className(其中可以存在由点分隔的任意数量的嵌套库):Stackutil库的一部分,是java库的一部分,因此:

import java.util.Stack