使用堆栈错误进行Postfix评估(java)

时间:2015-03-03 07:38:09

标签: java stack postfix-notation

所以我正在编写一个程序来评估文件中的后缀表达式。但是我一直收到错误:

 0
Exception in thread "main" java.lang.NumberFormatException: For input string: "+"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:484)
    at java.lang.Integer.valueOf(Integer.java:582)
    at postfixEvaluater.postfixEvaluation(postfixEvaluater.java:89)
    at postfixEvaluater.readFromTheFile(postfixEvaluater.java:49)
    at postfixEvaluater.main(postfixEvaluater.java:21)

所以我猜这与我的运营商有关。

输入.txt文件的内容如下:

 511+93/2-932/-149657%/*-+14+96*5/7%-149-+6*57%/ 

,预期输出为:

 5
 2
 1
 8
-4
 2
-4

到目前为止,这是我的所有代码:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class postfixEvaluater {
   public static void main(String[] commandlineArguments) {
      //Error Checking
      if(commandlineArguments.length == 0){
         System.out.println("Please enter the file name.");
      }
      else{
         postfixEvaluater.readFromTheFile(commandlineArguments[0]);
      }

   }        


   public static void readFromTheFile(String fileName) {
         //connect to file 
      File file = new File(fileName);
      Scanner scanFile = null;
      try {
         scanFile = new Scanner(file);
      } 
      catch (FileNotFoundException exception) {

         System.out.print("File not found for " + fileName);

         System.exit(1);
      }        

      while (scanFile.hasNextLine()) {

         String line = scanFile.nextLine();

         Integer postfixLine = postfixEvaluater.postfixEvaluation(line);
         System.out.println(postfixLine);



      }
   }


   public static Integer postfixEvaluation(String input){




      StackInterface<Integer> stack = new LinkedStack<Integer>();

      Integer result = new Integer(0);



      for(int i=0;i<input.length();i++){

         String characterNum = input.substring(i, i+1);

         Integer integer1 = new Integer(0);
         Integer integer2 = new Integer(0);

        Integer character = (Integer.valueOf(characterNum));



         if (character.equals("0")) {
            stack.push(character);

         }
         else if (character.equals("1")) {
            stack.push(character);

         }
         else if (character.equals("2")) {
            stack.push(character);
         }
         else if (character.equals("3")) {
            stack.push(character);
         }
         else if (character.equals("4")) {
            stack.push(character);
         }
         else if (character.equals("5")) {
            stack.push(character);
         }
         else if (character.equals("6")) {
            stack.push(character);
         }
         else if (character.equals("7")) {
            stack.push(character);
         }
         else if (character.equals("8")) {
            stack.push(character);
         }
         else if (character.equals("9")) {
            stack.push(character);
         }
         else if (character.equals("*")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 * integer2;


            stack.push(result);



         }
        else if (character.equals("/")) {
            integer2 = stack.pop();
            integer1 = stack.pop();

             result = integer1 / integer2;


            stack.push(result);

         }
        else if (character.equals("%")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 % integer2;


            stack.push(result);
          }
         else if (character.equals("+")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 + integer2;


            stack.push(result);

         }
         else if (character.equals("-")) {
            integer2 = stack.pop();
            integer1 = stack.pop();

             result = integer1 - integer2;


            stack.push(result);

         }


      }
      return result;   
   }
}

这是我的代码中我觉得问题所在的部分(顺便说一句,我推动并弹出除了整数之外):

public static Integer postfixEvaluation(String input){




      StackInterface<Integer> stack = new LinkedStack<Integer>();

      Integer result = new Integer(0);



      for(int i=0;i<input.length();i++){

         String characterNum = input.substring(i, i+1);

         Integer integer1 = new Integer(0);
         Integer integer2 = new Integer(0);

        Integer character = (Integer.valueOf(characterNum));



         if (character.equals("0")) {
            stack.push(character);

         }
         else if (character.equals("1")) {
            stack.push(character);

         }
         else if (character.equals("2")) {
            stack.push(character);
         }
         else if (character.equals("3")) {
            stack.push(character);
         }
         else if (character.equals("4")) {
            stack.push(character);
         }
         else if (character.equals("5")) {
            stack.push(character);
         }
         else if (character.equals("6")) {
            stack.push(character);
         }
         else if (character.equals("7")) {
            stack.push(character);
         }
         else if (character.equals("8")) {
            stack.push(character);
         }
         else if (character.equals("9")) {
            stack.push(character);
         }
         else if (character.equals("*")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 * integer2;


            stack.push(result);



         }
        else if (character.equals("/")) {
            integer2 = stack.pop();
            integer1 = stack.pop();

             result = integer1 / integer2;


            stack.push(result);

         }
        else if (character.equals("%")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 % integer2;


            stack.push(result);
          }
         else if (character.equals("+")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 + integer2;


            stack.push(result);

         }
         else if (character.equals("-")) {
            integer2 = stack.pop();
            integer1 = stack.pop();

             result = integer1 - integer2;


            stack.push(result);

         }


      }
      return result;   
   }

1 个答案:

答案 0 :(得分:1)

您的问题是以下代码行:

Integer character = (Integer.valueOf(characterNum));

由于您尝试将任何字符转换为数字而您的字符串也包含运算符(无法转换为数字),因此会出现异常。

你应该这样考虑一下:

  • 我的角色是一个数字?

    是=&gt;将其转换为数字并将其推入堆栈

    否=&gt;它是一个操作数,因此从堆栈中弹出前2个元素,执行操作并推送结果。