Domanda

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?

È stato utile?

Soluzione 3

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

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top