2015-04-03 55 views
-1

所以我正在研究一個PostFix計算器,它在命令行中用於類項目,而且我在爲它開發內存時遇到了一些麻煩。我被告知要創建一個hashMap,並且我已經研究了它並理解它的基礎知識。我有計算方法工作,但是我嘗試實現用戶聲明變量的方式時遇到了問題。例如該用戶應該能做什麼:如何在我的代碼中實現HashMap,我試圖將它用作內存?

> a = 3 5 + 1 - 
7 
> bee = a 3 * 
21 
> a bee + 
28 
> bee 3 % 
0 
> a = 4 
4 
> 57 
57 
> 2 c + 
c not found 
> mem 
a: 4 
bee: 21 
> exit 

正如你可以看到,用戶可以申報格式變量「=」 我的問題是,我真的不知道如何實現HashMap中,我試圖通過從數組列表中獲取哈希映射的變量名,並通過從我的計算方法中獲取返回值來從中獲取整數值,但我得到的只是這個錯誤:

>Exception in thread "main" java.lang.NumberFormatException: For input string: " 
Error" 
     at java.lang.NumberFormatException.forInputString(Unknown Source) 
     at java.lang.Integer.parseInt(Unknown Source) 
     at java.lang.Integer.parseInt(Unknown Source) 
     at Program6.main(Program6.java:42) 

這裏是我的代碼目前:

import java.util.*; 
import java.io.*; 
public class Program6 
{ 
    private static HashMap<String,Integer> memory = new HashMap<>(); 
    public static void main(String args[]) 
    { 
     System.out.println("Servando Hernandez"); 
     System.out.println("RPN command line calculator"); 
     Scanner scan = new Scanner(System.in); 
     System.out.print(">"); 
     while(scan.hasNextLine()) 
     { 
      System.out.print("> "); 
      String a = scan.nextLine(); 
      String b = "quit"; 
      String c = "mem"; 
      String d = "clear"; 

      if(a.equals(b)) 
      { 
       System.exit(0); 
      } 
      else 
      { 
       System.out.println(compute(a)); 
      } 
      System.out.print(">"); 

      List<String> list = new ArrayList<String>(); 
      if(!a.isEmpty()) 
      { 
       StringTokenizer var = new StringTokenizer(a); 
       while(var.hasMoreTokens()) 
       { 
        list.add(var.nextToken()); 
       } 
      } 
      int pos = 0; 
      if (compute(a) != null) 
      { 
       pos = Integer.parseInt(compute(a)); 
      } 



      memory.put(list.get(list.size()-1),pos); 


     } 


    } 



    public static String compute(String input) 
    { 
     List<String> processedList = new ArrayList<String>(); 
     if (!input.isEmpty()) 
     { 
      String myRegex = "[^a-zA-Z]"; 
      StringTokenizer st = new StringTokenizer(input); 
      while (st.hasMoreTokens()) 
      { 
       processedList.add(st.nextToken()); 
       processedList.remove(myRegex); 
       processedList.remove("="); 

      } 
     } 
     else 
     { 
      return "Error"; 
     } 
     Stack<String> tempList = new Stack<String>(); 

     Iterator<String> iter = processedList.iterator(); 

     while (iter.hasNext()) 
      { 
       String temp = iter.next(); 
       if (temp.matches("[0-9]*")) 
        { 

        tempList.push(temp); 
        } 
        else if (temp.matches("[*-/+]")) 
        { 

         if (temp.equals("*")) 
         { 
          int rs = Integer.parseInt(tempList.pop()); 
          int ls = Integer.parseInt(tempList.pop()); 
          int result = ls * rs; 
          tempList.push("" + result); 
         } 
         else if (temp.equals("-")) 
         { 
          int rs = Integer.parseInt(tempList.pop()); 
          int ls = Integer.parseInt(tempList.pop()); 
          int result = ls - rs; 
          tempList.push("" + result); 
         } 
         else if (temp.equals("/")) 
         { 
          int rs = Integer.parseInt(tempList.pop()); 
          int ls = Integer.parseInt(tempList.pop()); 
          int result = ls/rs; 
          tempList.push("" + result); 
         } 
         else if (temp.equals("+")) 
         { 
          int rs = Integer.parseInt(tempList.pop()); 
          int ls = Integer.parseInt(tempList.pop()); 
          int result = ls + rs; 
          tempList.push("" + result); 
         } 

        } 
        else 
        { 
         return "Error"; 
        } 
      } 

     return tempList.pop(); 
    } 
} 

有誰知道我可以如何讓後期修復計算器上的哈希映射內存工作到用戶可以分配變量並能夠調用它們的位置,或者更好的方法來處理?

+0

你究竟想把什麼放在散列表? – ryekayo 2015-04-03 15:06:56

+0

我正在嘗試存儲用戶的答案。用戶能夠以「variable = value」的格式聲明他自己的變量類型,就像我上面顯示的那樣。但我堅持如何使這項工作 – Dayman 2015-04-03 15:15:16

+1

你應該創建一個[MCVE](http://stackoverflow.com/help/mcve)。 – StackFlowed 2015-04-03 15:26:09

回答

0

你的問題是你在計算方法中添加了你的else子句中的「Error」,然後試圖把它解析爲一個int。

public static String compute(String input) 
{ 
    List<String> processedList = new ArrayList<String>(); 
    if (!input.isEmpty()) 
    { 
     String myRegex = "[^a-zA-Z]"; 
     StringTokenizer st = new StringTokenizer(input); 
     while (st.hasMoreTokens()) 
     { 
      processedList.add(st.nextToken()); 
      processedList.remove(myRegex); 
      processedList.remove("="); 

     } 
    } 
    else 
    { 
     return "Error"; //-->> problem 
    } 

將其解析爲int。這點compute(a)不會爲空,因爲它有「Error」。 下一步你試圖把它解析爲一個int。

if (compute(a) != null) 
{ 
    pos = Integer.parseInt(compute(a)); 
} 

你可以改變你的代碼

if (compute(a) != null && !compute(a).equals("Error")) 
{ 
    pos = Integer.parseInt(compute(a)); 
} 

你也應該嘗試你把你的Integer.parseInt()代碼在嘗試捕捉。

+0

對,但是我的程序返回錯誤,因爲在進入我的計算函數之前,我一直無法找到一種方法來刪除用戶輸入的變量(因爲我之前不知道)。 – Dayman 2015-04-03 15:18:25

+0

我只是說不要嘗試和解析一個int。當您嘗試執行Integer.parseInt(「Error」); // - >這會引發NumberFormatException。你不想要的。 – StackFlowed 2015-04-03 15:20:00

+0

好吧,那麼這是否意味着我應該刪除錯誤返回並嘗試解析變量以將其存儲在散列映射中,然後在計算我的計算結果後將結果存儲在我的散列映射中(使用變量)? – Dayman 2015-04-03 15:47:38

0

您的HashMap可用於存儲變量名稱及其值。地圖的關鍵字是變量名稱,值是分配給它的編號。您目前有Integer,但如果您要允許諸如a = 10 3 /之類的內容,您可能需要處理小數的內容。

在你compute(..)方法你期望的輸入是形式var = <calculation>的,所以你應該首先解析出變量名,這將作爲HashMap的內存使用的密鑰,並計算與memory.put(var,result);導致計算,存儲後。

當計算<calculation>部分時,如果遇到變量名稱,請使用memory.get(var)查找它的值並在計算中使用該值。使用後綴計算器,您只需獲取2個值,然後執行操作並執行數學運算。結果是下一對操作的第一個值,依此類推,直到操作完成並最終將結果賦給變量。

+0

好吧,這很有道理!那麼這是否意味着我應該擺脫我的錯誤返回? – Dayman 2015-04-03 15:45:40

+0

你應該經常檢查錯誤,這樣如果有人輸入了無效的東西,你的程序會做一些合理的事情。 – 2015-04-03 15:58:06

相關問題