NumberFormatException
Hi,
Below is a method I made that takes in a postfix expression and computs it. For some reason I'm getting a NumberFormatException: For imput string: "*" . Can anyone help me?
/**
* Method that computes a postfix expression
* @param postfix
* @return the result of the computation
* @throws PostfixArithmeticException
*/
publicdouble computeResult(String postfix)throws PostfixArithmeticException{
//local variables
Stack computePostfix =new Stack();
int countOperators = 0;
int countOperands = 0;
//goes through the postfix and determines number of operators and operands
for(int i = 0; i < postfix.length(); i++){
char ch = postfix.charAt(i);
if(ch =='+' || ch =='-' || ch =='*' || ch =='/' || ch =='%'){
countOperators++;
}
else
countOperands++;
}
//test if there are enough operators and operands, number of operators should be 1 less than operands
if((countOperators + 1) != countOperands){
//throw PostfixArithmeticException if there are not enough operators or operands
thrownew PostfixArithmeticException("Unable to compute postfix expression.");
}
//otherwise, compute postfix expresstion
for(int i = 0; i < postfix.length(); i++){
char ch = postfix.charAt(i);
//if the character is not a operator, push onto a stack
if(ch !='+' || ch !='-' || ch !='*' || ch !='/' || ch !='%'){
computePostfix.push("" + ch);
}
//otherwise, if an operator, pop two operands off the stack, compute with operator and push back on stack
else{
double calculation = Double.parseDouble("" + computePostfix.pop()) + ch + Double.parseDouble("" + computePostfix.pop());
computePostfix.push("" + calculation);
}
}
//return the result of the computation
this.result = this.result + Double.parseDouble("" + computePostfix.pop());
return result;
}
Thanks
[3802 byte] By [
jthepsena] at [2007-11-26 23:58:07]

> //if the character is not a operator, push onto a stack
> if(ch != '+' || ch != '-' || ch != '*' || ch != '/' || ch != '%') {
>computePostfix.push("" + ch);
>}
Whoops!//if the character is not a operator, push onto a stack
if(ch != '+' && ch != '-' && ch != '*' && ch != '/' && ch != '%') {
computePostfix.push("" + ch);
}
=D Okay it seems that as I get one problem fixed, another one occurs. The code where I compute the results and pop it back on. Since in a stack the numbers should be reversed so here's the new code for it:
else {
double operand2 = Double.parseDouble("" + computePostfix.pop());
double operand1 = Double.parseDouble("" + computePostfix.pop());
System.out.println(operand1 + " " + ch + " " + operand2);
this.result = operand1 + ch + operand2;
System.out.println(this.result);
//System.out.println(calculation);
computePostfix.push("" + result);
}
Say if I were to enter an argument 6-3 as an infix, my infixToPostfix method returns a postfix string 63-. The result I got is a double with a value of 54. Which is obviously wrong so I printed the operands and the operator before the computation, then printed the results after the computation (as shown in code) I get 6 - 3, but for results I get 54. What am I doing wrong in here?
> this.result = operand1 + ch + operand2;
I read this line before I got to your description of the problem and I was thinking
"What's result? operands 1 and 2 are doubles, and ch is a character." You could
look it up, or someone here might say, but really whatever this expression evaluates
to, it'll be wrong because it just makes no sense!
> The result I got is a double with a value of 54. Which is obviously wrong so I printed
> the operands and the operator before the computation, then printed the results
> after the computation (as shown in code) I get 6 - 3
What you have here is 2 numbers (6 and 3) and a character that stands for an
operation. Clearly what you have to do (in this case) is subtract 3 from 6 to
get result. In the general case a switch will be useful...