Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在我的代码中实现HashMap,我正在尝试将其用作内存?_Java_Hashmap_Calculator_Stringtokenizer_Postfix Notation - Fatal编程技术网

Java 如何在我的代码中实现HashMap,我正在尝试将其用作内存?

Java 如何在我的代码中实现HashMap,我正在尝试将其用作内存?,java,hashmap,calculator,stringtokenizer,postfix-notation,Java,Hashmap,Calculator,Stringtokenizer,Postfix Notation,所以我正在为一个类项目的命令行中使用的后缀计算器工作,我在为它开发内存方面遇到了一些麻烦。我被告知要创建一个hashMap,我已经对它进行了研究并了解了它的基本原理。我的计算方法可以工作,但我在尝试实现一种让用户声明变量的方法时遇到了困难。例如,这是用户应该能够做到的: > a = 3 5 + 1 - 7 > bee = a 3 * 21 > a bee + 28 > bee 3 % 0 > a = 4 4 > 57 57 > 2 c +

所以我正在为一个类项目的命令行中使用的后缀计算器工作,我在为它开发内存方面遇到了一些麻烦。我被告知要创建一个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,我尝试通过设置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();
    }
}
import java.util.*;
导入java.io.*;
公共课程6
{
私有静态HashMap内存=新HashMap();
公共静态void main(字符串参数[])
{
System.out.println(“Servando Hernandez”);
System.out.println(“RPN命令行计算器”);
扫描仪扫描=新扫描仪(System.in);
系统输出打印(“>”);
while(scan.hasNextLine())
{
系统输出打印(“>”);
字符串a=scan.nextLine();
字符串b=“退出”;
字符串c=“mem”;
字符串d=“清除”;
如果(a等于(b))
{ 
系统出口(0);
}
其他的
{
系统输出println(计算(a));
}
系统输出打印(“>”);
列表=新的ArrayList();
如果(!a.isEmpty())
{
StringTokenizer var=新的StringTokenizer(a);
while(变量hasMoreTokens())
{
添加(变量nextToken());
}
}
int pos=0;
如果(计算(a)!=null)
{
pos=整数.parseInt(计算(a));
}
memory.put(list.get(list.size()-1),pos);
}   
}
公共静态字符串计算(字符串输入)
{
List processedList=new ArrayList();
如果(!input.isEmpty())
{
字符串myRegex=“^a-zA-Z]”;
StringTokenizer st=新的StringTokenizer(输入);
而(st.hasMoreTokens())
{
processedList.add(st.nextToken());
processedList.remove(myRegex);
processedList。删除(“=”);
}
} 
其他的
{
返回“错误”;
}
Stack templast=new Stack();
迭代器iter=processedList.Iterator();
while(iter.hasNext())
{
字符串温度=iter.next();
如果(温度匹配(“[0-9]*”)
{
圣堂武士推(临时);
}
else if(温度匹配(“[*-/+]”)
{
如果(温度等于(“*”)
{
int rs=Integer.parseInt(templast.pop());
int ls=Integer.parseInt(templast.pop());
int结果=ls*rs;
圣堂武士推送(“+结果);
} 
else if(温度等于(“-”))
{
int rs=Integer.parseInt(templast.pop());
int ls=Integer.parseInt(templast.pop());
int结果=ls-rs;
圣堂武士推送(“+结果);
} 
else if(温度等于(“/”)
{
int rs=Integer.parseInt(templast.pop());
int ls=Integer.parseInt(templast.pop());
int结果=ls/rs;
圣堂武士推送(“+结果);
} 
else if(温度等于(“+”))
{
int rs=Integer.parseInt(templast.pop());
int ls=Integer.parseInt(templast.pop());
int结果=ls+rs;
圣堂武士推送(“+结果);
}
}
其他的
{
返回“错误”;
}
}
return templast.pop();
}
}

有人知道我如何让post fix计算器上的hashMap内存工作到用户可以分配变量并能够调用变量的位置,或者是更好的方法

您的问题是在compute方法中添加来自you 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 
    }
您可以根据需要更改代码

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

您还应该尝试将Integer.parseInt()代码放入try catch中。

您的
HashMap
可用于存储变量名及其值。映射的键是变量名,值是分配给它的数字。您当前有
Integer
,但如果要允许
a=103/
之类的操作,您可能需要处理小数的东西

在您的
compute(…)
方法中,您希望输入的形式是
var=
,因此您应该首先解析出变量名,该变量名将是hashmap内存中使用的键,在计算完后,将该结果存储在
memory.put(var,result)中

在计算
部分时,如果遇到变量名,请使用
memory.get(var)
查找并在计算中使用该值。使用后缀计算器,你只需要得到t
if (compute(a) != null && !compute(a).equals("Error"))
{
    pos = Integer.parseInt(compute(a));
}