why would any one call token.charAt(0) on string 5 ?

or 2? or 3? whats the use of that?

Okay to help you understand, lets break down on of the "RPN" strings from the "main" method...

`"123 6 - 5 +"`

The tokenizer runs and the first "token" is "123"

So we check the first character of "123" and find it is "1"...

Then we check to see if "1" matches one of our switch cases (" + ", " - " , " * ") which it does not...

Next, we grab the next token which is "6" and we check the first character again which is now "6"

And Then we check to see if "6" matches one of our switch cases (" + ", " - " , " * ") which again does not...

Now remember these numbers are going into ( being "pushed" into ) a Stack, so right now we have two in our Stack

123 and 6...

So let continue...

Now we grab the next token "-" and check the first character which is "-"... and you know what next, right ?

Of course, we check to see if "-" matches one of our switch cases (" + ", " - " , " * ") which this time it does...

Now looking at that switch statement for " - " we see that we grab ( "pop" ) the last two values "pushed" in our stack ( "123" and "6") and subtract them... The result "117" get pushed in to our Stack...

Since we "poped" two numbers ( "123" and "6" ) and they were the only ones, the Stack was emptied, and after doing the subtraction, we pushed the result "117" into the Stack

So our Stack now holds only "117"

So what next, well once again we grab the next token "5" and check the first character "5"...

and once again we check to see if it matches one of our cases (" + ", " - " , " * "), and it doesn't

so "5" gets "pushed" into the stack...

Now our stack has "117" and "5"...

What do you think is going to happen next ???

Right, we get our next token which is "

*" and check the first character "*"

and check to see if " + " matches one of our switch statements (" + ", " - " , " * "), yup... it does...

Looking at the switch statement for "+" we see that we are to "pop" the last two numbers "pushed" into our Stack ( "117" and "5") and add them together... which is "122" and "push" that into our Stack...

So now our Stack holds "122" only... What happens next...

We try to grab the next token, but there are no more, so empty handed we return to our switch statements and obviously we can not match any of these cases " + ", " - " , " * " so we end up defaulting to the "default" case which states "pop" that last values "pushed" into our Stack, in this case "122" and display our result...

Now that is the relative play by play,

But this class design leaves alot to be desired because we are using an thrown exception to bring us to our switch statement, while it works, it not pretty... But that another matter...

That said, I included several System.out.println() statements in your code so that you can see some of the process...

**import** java.util.*;**public** **class** RPN { **public** RPN() {} **public** **void** process(String s) { System.out.println("Processing RPN |" + s +"|"); StringTokenizer st = **new** StringTokenizer(s); Stack stack = **new** Stack(); **while**(st.hasMoreTokens()) { String token = st.nextToken(); **try** { stack.push(**new** Integer(token)); System.out.println("New Number Processed " + token); } **catch**(NumberFormatException e) { **int** v1, v2; **switch**(token.charAt(0)){ /// HERE what is this for? **case** '*': //and this stupid cases? v2 = ((Integer)stack.pop()).intValue(); v1 = ((Integer)stack.pop()).intValue(); System.out.println("Multiplying " + v1 + " * " + v2); System.out.println("Product Processed " + (v1*v2)); stack.push(**new** Integer(v1*v2)); **break**; **case** '+': v2 = ((Integer)stack.pop()).intValue(); v1 = ((Integer)stack.pop()).intValue(); System.out.println("Adding " + v1 + " + " + v2); System.out.println("Sum Processed " + (v1+v2)); stack.push(**new** Integer(v1+v2)); **break**; **case** '-': v2 = ((Integer)stack.pop()).intValue(); v1 = ((Integer)stack.pop()).intValue(); System.out.println("Subtracting " + v1 + " - " + v2); System.out.println("Difference Processed " + (v1-v2)); stack.push(**new** Integer(v1-v2)); **break**; **default**: System.out.println("Command " + token + " not understood");}}} System.out.println("Value on stack is " + stack.pop()); } **public** **static** **void** main(String[] args) { RPN rpn = **new** RPN(); rpn.process("2 3 + 4 - 2 *"); rpn.process("123 6 - 5 +"); }}

When it hit you, sit back and enjoy that lucid feeling of enlightenment...

Have fun...

- MaxxDmg...

- ' He who never sleeps... '