2015-03-03 56 views
1

所以我正在製作一個程序來評估文件中的後綴表達式。但我不斷收到錯誤:使用堆棧錯誤的後綴評估(java)

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; 
    } 
+2

這個例外很明顯。在你的postfixEvaluation方法中,你試圖將「+」號轉換爲int – DeiAndrei 2015-03-03 07:50:15

+0

@DeiAndrei所以所有的操作符都應該是Strings? – CMcorpse 2015-03-03 07:51:46

+1

操作數是整數(或任何數字類型),但運算符(+ - * /)不是。 – DeiAndrei 2015-03-03 07:53:22

回答

1

您的問題是下面的代碼行:

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

既然你想任何字符轉換爲數字和您的字符串也包含運營商(不能轉換爲數字),你會得到一個例外。

你或許應該考慮一下這種方式:

  • 是我的性格是數字嗎?

    是=>將其轉換爲數字,並將它推棧

    否=>這是一個操作數上,因此彈出從堆棧頂部2的元素,執行該操作和推的結果。

+0

所以字符只是作爲字符串characternum,並在數字的if和if else語句中,將其轉換爲整數呢? – CMcorpse 2015-03-03 08:02:38

+0

啊。想通了,非常感謝。 – CMcorpse 2015-03-03 08:08:52