Question

Okay, so here's the line of code I'm having trouble with:

double endNumber = (double)numbers.pop();
return endNumber;

My problem is that I need the number that pops off the stack as a double, but I keep getting the error:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double

Is there anyway to get around this?

Was it helpful?

Solution 3

You need to convert the item that you popped off the stack into a double:

double endNumber = Double.parseDouble(numbers.pop());
return endNumber;

OTHER TIPS

Your pop() method returns String, and, you have to parse it to double

double endNumber = Double.parseDouble(numbers.pop());
return Double.parseDouble(numbers.pop());

You ll have to use Double.parseDouble(argument)

double endNumber = Double.parseDouble(numbers.pop());

or you can directly return without using any double variable.

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