Java Get()方法第一次返回正确的值,第二次返回null

Java Get()方法第一次返回正确的值,第二次返回null,java,hashmap,Java,Hashmap,我使用哈希映射来存储键值对。当我使用get方法检索名为“Ape”的标识符对象键时,它正确地返回设置的对象值“1,2,3,4”,而当我尝试检索同一对象时,该值返回“” 我已经尝试确保我没有以某种方式删除值键对,并检查我是否每次都创建一个新的hashmap。当我打印hashmap-to-string方法时,标识符对象“Ape”不再与其值一起出现在映射中 抱歉,代码太多了!我不确定是否要全部加进去 package nl.vu.labs.phoenix.ap; import java.math.Big

我使用哈希映射来存储键值对。当我使用get方法检索名为“Ape”的标识符对象键时,它正确地返回设置的对象值“1,2,3,4”,而当我尝试检索同一对象时,该值返回“”

我已经尝试确保我没有以某种方式删除值键对,并检查我是否每次都创建一个新的hashmap。当我打印hashmap-to-string方法时,标识符对象“Ape”不再与其值一起出现在映射中

抱歉,代码太多了!我不确定是否要全部加进去

package nl.vu.labs.phoenix.ap;

import java.math.BigInteger;
import java.util.*;
import java.util.regex.Pattern;
import java.io.*;

/**
 * A set interpreter for sets of elements of type T
 */
public class Interpreter<T extends SetInterface<BigInteger>> implements InterpreterInterface<T> {

    HashMap<IdentifierInterface, T> hm = new HashMap<IdentifierInterface, T>();
    @Override
    public T getMemory(String v) {
        IdentifierInterface i = new Identifier();
        i.appendIdentifier(v);
        if(hm.containsKey(i)){
            return hm.get(i);
        }else{

        return null;
        }
    }

    @Override
    public T eval(String s) {
        System.out.println("HASHMAP:"+hm.toString());
        Scanner in;
        try {
            in = format(s);
        } catch (APException e1) {
            in=new Scanner(s);
        }
        in.useDelimiter("");
        try {
            readStatement(in);

        }
        catch(Exception e) {
                return null;
        }

        return null;
    }
            char nextChar(Scanner in) {
                return in.next().charAt(0);
            }

            boolean nextCharIs(Scanner in, char c) {
                boolean IsMatch = in.hasNext(Pattern.quote(c+""));
                return IsMatch;
            }

            boolean nextCharIsDigit (Scanner in) {
                return in.hasNext("[0-9]");
            }

            boolean nextCharIsLetter (Scanner in) {
                return in.hasNext("[a-zA-Z]");
            }

            void eraseSpace(Scanner in) {
                while (nextCharIs(in, ' ')) {
                    nextChar(in);
                }
            }

            private void checkChar (String in, char a) throws APException {
                Scanner token = new Scanner(in);
                if (! nextCharIs(token, a)) {
                    throw new APException("Missing token: " + a);
                }

            }

            private String SetToString(SetInterface<BigInteger> set){
                StringBuilder output = new StringBuilder();

                if(set.first()){
                    output.append(set.get());

                    while(set.next()){
                        output.append(" ");
                        output.append(set.get());
                    }
                }
                return output.toString();
            }

            private Scanner format(String statement) throws APException {

                Scanner in = new Scanner(statement);
                in.useDelimiter("");
                StringBuilder line = new StringBuilder();

                while (in.hasNext()) {

                    if (nextCharIs(in, '/')) {
                        line.append(in.nextLine());
                        return new Scanner(line.toString());
                    } else if (nextCharIsLetter(in) || nextCharIsDigit(in)) {
                        line.append(in.next());
                        while (nextCharIs(in, ' ')) {
                            checkChar(in.next(), ' ');

                            if (nextCharIsLetter(in) || nextCharIsDigit(in)) {
                                throw new APException("Identifier error: spaces inside identifier not allowed");
                            }
                        }
                    } else if (nextCharIs(in, ' ')) {
                        checkChar(in.next(), ' ');
                    } else {
                        line.append(in.next());
                    }
                }
                String formated = line.toString();
                for (int i = 0; i < formated.length(); i++) {
                    if (formated.charAt(i) == ',') {

                        try {
                            if (!Character.isDigit(formated.charAt(i - 1)) || !Character.isDigit(formated.charAt(i + 1))) {
                                throw new APException("no number in set");
                            }
                        } catch (Exception e) {
                            throw new APException("no number in set");
                        }
                    } else if (formated.charAt(i) == '0') {
                        if (!Character.isDigit(formated.charAt(i - 1)) && Character.isDigit(formated.charAt(i + 1))) {
                            throw new APException("Invalid number");
                        }
                    }
                }
                return new Scanner(line.toString());
            }
            void readStatement(Scanner in)throws APException{

                if (nextCharIsLetter(in)) {

                    readAssignment(in);

                } else if (nextCharIs(in, '?')) {
                    readPrint_statement(in);

                } else if (! nextCharIs(in, '/')){
                    throw new APException("Invalid statement, please read the documentation");
                }
            }

            void readAssignment(Scanner in) throws APException{
                //handle end of line

                in.useDelimiter("=");
                IdentifierInterface i;
                SetInterface<BigInteger> set;
                i=readIdentifier(in);
                set = readExpression(new Scanner(in.next()));
                this.hm.put(i,(T)set);



            }
            void readPrint_statement(Scanner in)throws APException{
                in.useDelimiter("");
                if(nextCharIs(in,'(')||nextCharIs(in,'{')||nextCharIsLetter(in)||nextCharIsDigit(in)){
                    readExpression(in);

                }else if (! nextCharIs(in, ';')){
                    throw new APException("Invalid statement, please read the documentation");
                }
            }

            IdentifierInterface readIdentifier(Scanner in)throws APException{

                  IdentifierInterface i = new Identifier();
                  String ident="";
                  if(in.hasNext()) {
                      ident+=in.next();

                  }
                  System.out.println("readident:"+ident);
                  i.appendIdentifier(ident);
                  if(i.hasCorrectIdentifierFormat(i.toString())){
                      return i;
                  }else{
                      throw new APException("Invalid Identifier foramt");
                  }

            }
            SetInterface<BigInteger> readExpression(Scanner in)throws APException{
                in.useDelimiter("");
                SetInterface<BigInteger> term = null;
                StringBuilder terms = new StringBuilder();
                while (in.hasNext()) {

                    while(nextCharIs(in, ' ')) {
                        in.next();
                    }
                    //System.out.println("HRE:"+in.next());
                    if (nextCharIs(in, '+') || nextCharIs(in, '-') || nextCharIs(in, '|')) {
                        String addOp = in.next();

                        if (term == null) {
                            term = readTerm(new Scanner(terms.toString()));

                        }
                        terms.setLength(0);

                        while (in.hasNext()) {

                            if ((nextCharIs(in, '+') || nextCharIs(in, '-') || nextCharIs(in, '|'))) {
                                term = addOps(term, readTerm(new Scanner(terms.toString())), addOp);
                                terms.setLength(0);
                                break;
                            } else {
                                terms.append(in.next());
                            }
                        }
                        if (terms != null) {
                            term = addOps(term, readTerm(new Scanner(terms.toString())), addOp);
                        }
                    } else {

                        terms.append(in.next());

                    }
                }

                if (term == null) {
                    term = readTerm(new Scanner(terms.toString()));

            }

                return term;
            }
            SetInterface<BigInteger> addOps(SetInterface<BigInteger> set1, SetInterface<BigInteger> set2, String op){
                System.out.print("addops");
                SetInterface<BigInteger> set = new Set<>();
                if(op=="+"){
                    set = set1.union(set2);
                }else if(op=="-"){
                    set = set1.difference(set2);    
                }else{
                    set = set1.symmetricDifference(set2);
                }
                return set;
            }


            SetInterface<BigInteger> readTerm(Scanner in)throws APException{
                in.useDelimiter("");
                SetInterface<BigInteger> factor;
                StringBuilder factors = new StringBuilder();


                while (in.hasNext()) {

                    if (nextCharIs(in, '*')) {
                        checkChar(in.next(), '*');
                        factor = readFactor(new Scanner(factors.toString())).intersection(readTerm(new Scanner(in.nextLine())));
                        return factor;
                    } else {
                        factors.append(in.next());
                    }
                }

                factor = readFactor(new Scanner(factors.toString()));
                return factor;
            }
            SetInterface<BigInteger> readFactor(Scanner in)throws APException{
                in.useDelimiter("");
                SetInterface<BigInteger> resultset = new Set<>();
                int complexFactors = 0;
                StringBuilder sets = new StringBuilder();

                while(in.hasNext()) {
                    if(nextCharIs(in,' ')){
                        in.next();
                    }
                    if (nextCharIs(in, '(')) {
                        checkChar(in.next(), '(');
                        complexFactors += 1;
                        while (in.hasNext() && complexFactors != 0) {

                            if (nextCharIs(in, '(')) {
                                complexFactors += 1;

                                sets.append(in.next());
                            } else if (nextCharIs(in, ')')) {

                                complexFactors -= 1;
                                sets.append(in.next());
                                if (complexFactors == 0) {
                                    if (nextCharIs(in,')')) {

                                        throw new APException("Invalid token detected");
                                    }
                                } else {

                                    if(!in.hasNext()&&complexFactors!=0){

                                        throw new APException("Invalid token detected");
                                    }
                                    else{
                                    sets.append(in.next());
                                    }
                                }
                            }else {
                                sets.append(in.next());
                            }
                        }
                        resultset = readExpression(new Scanner(sets.toString()));
                    } else if (nextCharIsLetter(in)) {
                        sets.append(in.next());

                        while (nextCharIsLetter(in)|| nextCharIsDigit(in)) {
                            sets.append(in.next());
                        }
                        IdentifierInterface i = new Identifier();
                        i.appendIdentifier(sets.toString());

                        if (hm.containsKey(i)) {
                            resultset = hm.get(i);
                            return resultset;
                        } else {
                            throw new APException("Identifier \\\""+ sets.toString() +"\\\" does not correspond to a sets");
                        }
                    } 
                    if (nextCharIs(in,'{')) {

                        checkChar(in.next(), '{');
                        if (nextCharIs(in,',')) {
                            throw new APException("no number");
                        }

                        while (!nextCharIs(in,'}') && in.hasNext()) {
                            sets.append(in.next());
                        }

                        if (in.hasNext()) {
                            checkChar(in.next(), '}');
                        } else {
                            throw new APException("Invalid token");
                        }

                        if (in.hasNext()) {
                            throw new APException("Operator or end of line missing");
                        }
                        resultset = readSet(sets.toString());
                    } else {
                        throw new APException("Invalid statement detected");
                    }
                }
                if (complexFactors != 0) {
                    throw new APException("Missing parenthesis detected");
                }

                return resultset;
            }

            SetInterface<BigInteger> readSet(String nums)throws APException{
                SetInterface<BigInteger> set = new Set<>();
                Scanner in = new Scanner(nums);
                in.useDelimiter(",");
                while (in.hasNext()) {
                    try {
                        set.add(in.nextBigInteger());
                    } catch (Exception e) {
                        throw new APException("Invalid number detected in a set");
                    }
                }
                in.close();
                //set.fixDoubles();
                return set;
            }


}

public void parenthesisBalanceTests() {
        InterpreterInterface<Set<BigInteger>> interpreter = new Interpreter<Set<BigInteger>>();

        interpreter.eval("Ape = {1, 2, 3, 4, 5, 6, 7, 8, 9}");
        // missing closing parenthesis test
        interpreter.eval("Ape = (({1, 2, 3})");

        SetInterface<BigInteger> actual = interpreter.getMemory("Ape");
        System.out.print("ACTRUAL:"+actual);
        ArrayList<BigInteger> expected = convertExpectedList("1 2 3 4 5 6 7 8 9");
        assertTrue("missing closing parenthesis' should result in an exception", compareSets(actual, expected));

        // missing opening parenthesis test
        interpreter.eval("Ape = ({1, 2, 3}))");
        actual = interpreter.getMemory("Ape");
        assertTrue("missing opening parenthesis' should result in an exception", compareSets(actual, expected));
    }
包nl.vu.labs.phoenix.ap;
导入java.math.biginger;
导入java.util.*;
导入java.util.regex.Pattern;
导入java.io.*;
/**
*T型元素集合的集合解释程序
*/
公共类解释器实现解释器接口{
HashMap hm=新的HashMap();
@凌驾
公共T getMemory(字符串v){
IdentifierInterface i=新标识符();
i、 附加标识符(v);
如果(hm.CONTANSKEY(i)){
返回hm.get(i);
}否则{
返回null;
}
}
@凌驾
公共T评估(字符串s){
System.out.println(“HASHMAP:+hm.toString());
扫描仪输入;
试一试{
in=格式;
}捕获(APException e1){
in=新扫描仪;
}
in.useDelimiter(“”);
试一试{
可读声明(in);
}
捕获(例外e){
返回null;
}
返回null;
}
字符nextChar(扫描仪输入){
返回.next().charAt(0);
}
布尔nextCharIs(扫描仪输入,字符c){
boolean IsMatch=in.hasNext(Pattern.quote(c+));
返回IsMatch;
}
布尔nextCharIsDigit(扫描仪输入){
返回.hasNext(“[0-9]”);
}
布尔NEXTCHARSLETTER(扫描仪输入){
返回.hasNext(“[a-zA-Z]”);
}
空白擦除空间(扫描仪输入){
while(nextCharIs(in.)){
nextChar(in);
}
}
私有void checkChar(字符串输入,字符a)抛出一个异常{
扫描器令牌=新扫描器(in);
如果(!nextCharIs(令牌,a)){
抛出新的APException(“缺少标记:+a”);
}
}
私有字符串SetToString(SetInterface集合){
StringBuilder输出=新的StringBuilder();
if(set.first()){
append(set.get());
while(set.next()){
输出。追加(“”);
append(set.get());
}
}
返回output.toString();
}
专用扫描程序格式(字符串语句)引发APException{
扫描仪输入=新扫描仪(报表);
in.useDelimiter(“”);
StringBuilder行=新的StringBuilder();
while(在.hasNext()中){
if(nextCharIs(in,“/”){
line.append(在.nextLine()中);
返回新扫描仪(line.toString());
}else if(nextCharIsLetter(in)| | nextCharIsDigit(in)){
line.append(in.next());
while(nextCharIs(in.)){
checkChar(in.next(),“”);
if(下一个哈里斯莱特(in)| |下一个哈里斯数字(in)){
抛出新APException(“标识符错误:标识符内不允许有空格”);
}
}
}else if(nextCharIs(in.)){
checkChar(in.next(),“”);
}否则{
line.append(in.next());
}
}
字符串格式化=line.toString();
for(int i=0;i