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?

有帮助吗?

解决方案 3

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

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top