2015-04-03 91 views
0

好吧我对java很陌生。我正在创建一个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 

用户使用“VAR =”来分配变量,以后可以用它来调出之前的答案。 我不能让我的计算方法忽略了“A =”,所以它返回一个错误,并且当我运行该程序,并尝试使用一个变量,我得到了错误

>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:38) 

有什么好办法以某种方式让我的计算方法忽略变量输入,以及实现散列表的最佳方式是什么?我似乎陷入了困境。这里是到目前为止

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 = Integer.parseInt(compute(a)); 



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

     } 


    } 



    public static String compute(String input) 
    { 
     List<String> processedList = new ArrayList<String>(); 
     if (!input.isEmpty()) 
     { 
      StringTokenizer st = new StringTokenizer(input); 
      while (st.hasMoreTokens()) 
      { 
       processedList.add(st.nextToken()); 
      } 
     } 
     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

我已经调试代码我的代码,找到比赛方法做了什么。

当你输入类似1+1matches("[0-9]*")matches("[*-/+]")都返回,所以代码输入else分支和return "Error";

而且,你把一个字符串“Error”到Integer.parseInt(),所以扔例外。

给我可怜的英语。

+0

对不起,我猜想大家都知道PostFix计算器是什么。用户必须输入每个参数的空格,并且操作符会在输入整数之后执行。 – Servanh 2015-04-03 04:07:56

+0

我输入'1 + 1',并在计算方法代码'else if(temp.equals(“+”)) {//你先弹出,你的tempList将为空,所以当你再次弹出时,异常 int rs = Integer.parseInt(tempList.pop()); int ls = Integer.parseInt(tempList.pop()); int result = ls + rs; tempList.push(“”+ result); }' – phony 2015-04-03 06:20:25

+0

如何在注释中创建新行,太难显示代码 – phony 2015-04-03 06:25:44