Question

I have a hashtable that consists of a String and Double A, 8.0 is an example of what would be in it. How do I access the 8.0? Do I just go hashtable.get(A)? I'm trying to get the value of two separate keys to add them together.

    NodeStack myStack=new NodeStack();
    for(int i=0; i<infix.length(); i++){
        char ch=infix.charAt(i);
        if(ch=='+'){
            Character one=(char) myStack.pop();
            Character two=(char) myStack.pop();

            double first=Double.parseDouble(numbers.get(one));//, second=numbers.get(two);
            double temp=first;//+second;
            myStack.push(temp);
        }
Was it helpful?

Solution

You access the value in a hashtable by using the get(key) method. In your case you could do the following:

Character one=(char) myStack.pop();
Character two=(char) myStack.pop();

String str1 = Character.toString(one);
String str2 = Character.toString(two);

double first=Double.parseDouble(numbers.get(str1));
double second=Double.parseDouble(numbers.get(str2));
double temp=first+second;
myStack.push(temp);

Since your hashtable key is a String you need to use a String in the get() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top