integer - Having trouble casting a stack object number to int (Java) -


i'm writing program converts expression infix postfix. have conversion part down when comes evaluating postfix expression, run problems converting char int using stack.

i keep getting error: "exception in thread "main" java.lang.classcastexception: java.lang.character cannot cast java.lang.integer"

it might in part of code below problem i'm not sure:

  integer x1 = (integer)stack2.pop();   integer x2 = (integer)stack2.pop(); 

thank you!

public class calc {  /**  * @param args command line arguments  */   public static void main(string[] args) {   system.out.println("please enter infix expression: ");     scanner scanner = new scanner(system.in); string input = scanner.nextline();  string postfix = ""; stack<character> stack = new stack<character>();  for(int = 0; < input.length(); i++){     char subject = input.charat(i);     if (subject == '*'||subject == '+'||subject == '-'||subject == '/'){                while ((stack.empty() == false) && (order(stack.peek()) >= order(subject)))       postfix += stack.pop() + " ";       stack.push(subject);                     }            else if(subject == '(')     {       stack.push(subject);     }     else if(subject == ')')     {       while(stack.peek().equals('(') == false){       postfix += stack.pop() + " ";     }       stack.pop();      }     else{         if((character.isdigit(subject) == true) && ((i + 1) < input.length()) && (character.isdigit(input.charat(i+1)) == true))         {             postfix += subject;         }         else if(character.isdigit(subject))         {             postfix += subject + " ";         }            else         {             postfix += subject;         }     } }  postfix += stack.pop(); system.out.println("your post-fix expression is: " + postfix);  char subject2; int yeah;  stack stack2 = new stack(); (int j = 0; j < postfix.length(); j++){     subject2 = postfix.charat(j);     if(character.isdigit(subject2) == true){          stack2.push(subject2);     }     if(subject2 == ' '){         continue;     }     else{         integer x1 = (integer)stack2.pop();         integer x2 = (integer)stack2.pop();         if (subject2 == '+'){             yeah = x1 + x2;         }         if (subject2 == '-'){             yeah = x1 - x2;         }         if (subject2 == '*'){             yeah = x1 * x2;         }         if (subject2 == '/'){             yeah = x1 / x2;         }         else {             yeah = 0;         }     } } yeah = (int) stack2.pop();  system.out.println("the result is:" + yeah);   } static int order(char operator) {     if(operator == '+' || operator =='-')     return 1;     else if(operator == '*' || operator == '/')      return 2; else     return 0; }     } 

you can solve casting int. note int , integer not same:

integer x1 = (int) stack2.pop(); integer x2 = (int) stack2.pop(); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -