简体   繁体   中英

Error: 'void' type not allowed here, within while loop (Java)

I keep getting the following error message in my method:

"Error: 'void' type not allowed here" on the line outEquation = outEquation + opSt.pop() + " ";.

The code I'm working on currently is a stacked linked list which takes in user input (in infix notation) and converts it to postfix. Any help would be appreciated.

import java.util.Scanner;

public class StackDemo
{
  public static void main(String[] args)
  {

    final int right = 0;
    final int left = 1;

    final int ADD = 0;
    final int MULT = 1;
    final int EXP = 2;
    final int PAR = -1;


  }

  public void UserPrompt()
  {
    Scanner keyboard = new Scanner(System.in);
    String input = keyboard.nextLine();

    System.out.println("Please select what type of conversion you would like to do:  ");
    System.out.println();
    System.out.println("1) Infix to postfix \n2) Postfix to infix \n3) Print Equations \n4) Exit");

    if(input == "1")
    {
      infix();
    }
    else if(input == "2")
    {
      postfix();
    }
    else if(input == "3")
    {
      print();
    }
    else if(input == "4")
    {
      System.exit(0);
    }
    else
    {
      System.out.println("That is not a correct input, please re-enter.");
      UserPrompt();
    }
  }


  public String infix()
  {
    String outEquation = "";
    LinkedStackClass<String> opSt = new LinkedStackClass<String>();


    Scanner keyboard = new Scanner(System.in);




    System.out.println("Please enter the infix equation:  ");

    while(keyboard.hasNext())
    {
      String str = keyboard.next();

      if(!isOperator(str))
      {
        outEquation = outEquation + str + " ";
      }
      else if(str.equals("("))
      {
        opSt.push(str);
      }
      else if(str.equals(")"))
      {
        while(!opSt.peek().equals("("))
        {
          outEquation = outEquation + opSt.pop() + " ";
        }
        opSt.pop();
      }
      else
      {
        while(opSt.size() > 0 && precede(opSt.peek(), str))
        {
          if(!opSt.peek().equals("("))
          {
            outEquation = outEquation + opSt.pop() + " ";
          }
          else
          {
            opSt.pop();
          }
        }
        if(!str.equals(")"))
        {
          opSt.push(str);
        }
      }
      while(opSt.size() > 0)
      {
        outEquation = outEquation + opSt.pop() + " ";
      }
    }
  }





    private static int getExpOrder(String op)
    {
      switch(op)
      {
        case "+":
        case "-":
        case "*":
        case "/":
          return left;

        case "^":
          return right;

          //default
      }
    }


    private boolean precede(String l, String r)
    {
      return (getPrec(l) > getPrec(r) || (getPrec(l) == getPrec(r) && getExpOrder(l) == left));
    }

    private int getPrec(String op)
    {
      switch(op)
      {
        case "+":
        case "-":
          return ADD;

        case "*":
        case "/":
          return MULT;

        case "^":
          return EXP;

        case "(":
        case ")":
          return PAR;
      }
    }



    public static boolean isOperator(String op)
    {
      return (op.length() == 1 && "+-*/()".indexOf(op.charAt(0)) != -1);
    }


    public String toString()
    {
      return outEquation;
    }

    public void postfix()
    {
      System.out.println("Postfix");
    }

    public void print()
    {
      System.out.println("Print");
    }
  }



public class LinkedStackClass<T> extends UnorderedLinkedList<T>
{
 public LinkedStackClass()
 {
  super();
 }

 public void initializeStack()
 {
    initializeList();
 }

    public boolean isEmptyStack()
 {
  return isEmptyList();
 }

 public boolean isFullStack()
 {
  return false;
 }

 public void push(T newElement)
 {
     insertFirst(newElement);
 } //end push

 public T peek() throws StackUnderflowException
    {
   if (first == null)
         throw new StackUnderflowException();

      return front();
     } //end peek

     public void pop()throws StackUnderflowException
     {
          if (first == null)
             throw new StackUnderflowException();

          first = first.link;

          count--;

          if (first == null)
             last = null;
     }//end pop
}

Ok, so the reason are you getting that error message is because your pop() function has a void return. Typically, in a stack implementation, the pop operation will remove the top item of the stack and return it. Your function only removes the element.

So change your pop() function to look as follows (I apologize in advanced, as Java is not my forté, and this may not even be correct, so you may need to tweak this):

public T pop() throws StackUnderflowException
{
    if (first == null)
        throw new StackUnderflowException();

    // Get the top-most element
    T top = peek();

    first = first.link;

    count--;

    if (first == null)
        last = null;

    return top;
} //end pop

However as user Ashley Frieze stated, better to use an existing implementation if possible, rather than roll your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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