错误:此处不允许'void'类型,while循环(Java)

时间:2015-04-06 17:50:55

标签: java java.util.scanner void

我的方法中不断收到以下错误消息:

  

"错误:'无效'这里不允许输入"在线上outEquation = outEquation + opSt.pop()+" &#34 ;;。

我目前正在处理的代码是一个堆叠链接列表,它接收用户输入(以中缀表示法)并将其转换为后缀。任何帮助将不胜感激。

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
}

1 个答案:

答案 0 :(得分:0)

好的,原因是您收到该错误消息是因为您的pop()函数返回void。通常,在堆栈实现中,pop操作将删除堆栈的顶部项并返回它。您的函数只删除元素。

所以改变你的pop()函数看起来如下(我在高级时道歉,因为Java不是我的强项,这甚至可能都不正确,所以你可能需要调整它):

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

然而,正如用户Ashley Frieze所说,如果可能的话,最好使用现有的实现,而不是自己动手。