PostFixCalculator while / try error

时间:2014-09-16 20:03:35

标签: java

import java.util.Scanner;

public class PostFixCalculator
{
      public static void main(String [] args)
      {
            Scanner kbd = new Scanner(System.in);
            int result;
            String expression;

            System.out.println("Student name, CS-304, Fall 2014, Asst 2c.");

            System.out.println("To quit this program, just hit 'return'.\n");

            System.out.print("Enter a postfix expression: ");
            expression = kbd.nextLine();

         while (!expression.equals(""))
         {
               try
               {
               }
            catch(RuntimeException e)
            {
            }

            System.out.print("\nEnter a postfix expression: ");
            expression = kbd.nextLine();

         } // end while (!expression.equals(""))

         System.out.println("\nBye!");

      } // end public static void main(String [] args)


   public static int postFixEvaluate(String input)
   {
      Scanner tokenizer; 
      int result, operand1, operand2, value;
      String operator;
      LinkedStack s = new LinkedStack();

      tokenizer = new Scanner(input);

      while (tokenizer.hasNext())
      {
         if (tokenizer.hasNextInt())
         {
            value = tokenizer.nextInt();
            s.push(value);
         }

         else // we have an operator
         {
            operator = tokenizer.next();

            if (s.isEmpty())
                throw new RuntimeException ("Not Enough Operands");
                operand2 = s.pop();

            if (s.isEmpty())
                throw new RuntimeException ("Not Enough Operands");
                operand1 = s.pop();

            if (operator.equals("+"))
               result = operand1 + operand2;
            else if (operator.equals("-"))
               result = operand1 - operand2;
            else if (operator.equals("*"))
               result = operand1 * operand2;
            else if (operator.equals("/"))
               result = operand1 / operand2;
            else
                throw new RuntimeException ("Not Enough Operands");

            s.push(result);
         } // end else // we have an operator

      } // end while (tokenizer.hasNext())

      if (s.isEmpty())
          throw new RuntimeException ("Not Enough Operands");

         result = s.pop();

      if (!s.isEmpty())
          throw new RuntimeException ("Not Enough Operands");

      return result;

   } // end public static int postFixEvaluate(String input)

} // end public class PostFixCalculator

我有这个代码,一个PostFixCalculator但是无论我放弃什么并尝试,我一直收到错误。该程序编译并运行它的发布方式,但它无法正常运行。我撞墙了..

3 个答案:

答案 0 :(得分:5)

你的while循环不正确。尝试重新访问并进行更正

答案 1 :(得分:3)

只是尝试在catch块中执行e.printStackTrace(),您将看到该错误是NullPointerException。如果您正在进行空捕获,则会丢失所有这些信息。

试试这个:

package test;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PostFixCalculator {
  public static void main(String[] args) {
    Scanner kbd = new Scanner(System.in);
    String expression;

    System.out.println("Student name, CS-304, Fall 2014, Asst 2c.");

    System.out.println("To quit this program, just hit 'return'.\n");

    System.out.print("Enter a postfix expression: ");
    expression = kbd.nextLine();

    while (!expression.equals("")) {
        try {
            System.out.println(postFixEvaluate(expression));
        } catch (RuntimeException e) {
            System.out.println(e.getLocalizedMessage());
        }

        System.out.print("\nEnter a postfix expression: ");
        expression = kbd.nextLine();

    } // end while (!expression.equals(""))

    System.out.println("\nBye!");
    kbd.close();

  } 

  public static String postFixEvaluate(String input)
  {

  List<String> operatorsAsStrings = getListOfValuesAsString(input.split("\\d+"));
  List<String> digitsAsStrings = getListOfValuesAsString(input.split("\\W+"));


  if(operatorsAsStrings.size() >= digitsAsStrings.size()){
      throw new RuntimeException("Incorrect format of expression.");
  }

  if(digitsAsStrings.size()<2){
      throw new RuntimeException("Not Enough Operands.");
  }

  BigInteger result = new BigInteger("0");
  boolean firstTime = true;

  for (int i=0; i<=digitsAsStrings.size()-1; i++) {

      if(firstTime){
          result = result.add(new BigInteger(digitsAsStrings.get(i)));
          firstTime = false;

      }else{

          String operator = operatorsAsStrings.get(i-1);
          if (operator.equals("+"))
              result = result.add(new BigInteger(digitsAsStrings.get(i)));
          else if (operator.equals("-"))
               result = result.subtract(new BigInteger(digitsAsStrings.get(i)));
          else if (operator.equals("*"))
               result = result.multiply(new BigInteger(digitsAsStrings.get(i)));
          else if (operator.equals("/"))
               result = result.divide(new BigInteger(digitsAsStrings.get(i)));

      }

  }
  return result.toString(); 
 } 

 private static List<String> getListOfValuesAsString(String[] split) {
    List<String> resultList = new ArrayList<>();

    for(String string: split){
        if(string != null && !string.isEmpty()){
            resultList.add(string);
        }
    }

    return resultList;
 }
}

答案 2 :(得分:1)

如果你调用postFixEvaluate,我会更好地工作: - )

尝试

while (!expression.equals(""))
{
    System.out.println(postFixEvaluate(expression));

(另一个替代方案,如果在问题中错过了调用,那就是LinkedStack出了问题 - 我用的是Stack)