tags:

views:

18

answers:

1

I have this code in a reserved word boolean format:

private boolean isIdent(String t) {
        if (equals(t, "final") || equals(t, "int") || equals(t, "while")
            || equals(t, "if") || equals(t, "else") || equals(t, "print")) return false;
        if (t!=null && t.length() > 0 && Character.isLetter(t.charAt(0))) return true;
        else return false;
    }

I need to turn this into a HashSet format but unsure how to approach this. Any help would be most appreciated.

A: 

You mean by putting the reserved words in a Set?

private Set<String> keywords;

private void initKeywords() {
    keywords = new HashSet<String>();
    keywords.add("final");
    keywords.add("int");
    keywords.add("while");
    keywords.add("if");
    keywords.add("else");
    keywords.add("print");     
}

private boolean isIdent(String t) {
    if (keywords.contains(t)) {
        return false;
    }
    else if (t != null && t.length() > 0 && Character.isLetter(t.charAt(0))) {
        return true;
    }
    else {
        return false;
    }
}
Kaleb Brasee
Hi Kaleb, Thanks so much this was very informative