1 Replies - 1180 Views - Last Post: 11 October 2012 - 08:45 AM Rate Topic: -----

#1 breiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 28-February 12

Stacks

Posted 11 October 2012 - 08:11 AM

Here are pieces of my code..program uses a GUI to get an expression then evaluate it using 2 stacks, valueStack and operatorStack. There is an error with the operatorStack.push(thisOp) saying that java.lang.Integer is required but String is found. Also do I need to write the .peek() method since I am using the BoundedStackInterface

public class ExpressionGUI extends JFrame implements ActionListener{
  JLabel                              inputLabel,  outputLabel;
  JButton                             enterButton, clearButton;
  JTextField                          inputText,   outputText;
  String                              expression,  nextToken;
  BoundedStackInterface1<Integer>     valueStack,  operatorStack;
  
  public ExpressionGUI(){
    setLayout(new FlowLayout());
    valueStack    = new ArrayStack1<Integer>();
    operatorStack = new ArrayStack1<Integer>();



void calculate(String expression){
      String thisOp;
      char Op;
      int result;
      Scanner tokenizer = new Scanner(expression);
      Object temp1, temp2;
      
      result = 0;
      while(tokenizer.hasNext()){
        if(tokenizer.hasNextInt()){
           valueStack.push(tokenizer.nextInt());
          } 
        else{
          thisOp = tokenizer.next();
          operatorStack.push(thisOp);
          Op = thisOp.charAt(0);
          
          if(thisOp < operatorStack.peek()){
            temp1 = valueStack.pop();
            temp2 = valueStack.pop();



Is This A Good Question/Topic? 0
  • +

Replies To: Stacks

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,134
  • Joined: 05-April 11

Re: Stacks

Posted 11 October 2012 - 08:45 AM

thisOp is a String but you can only put Integer objects in your stack

You told it to only take Integer objects when you declared the stack
BoundedStackInterface1<Integer>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1