# [Java] Making a RPN calculator



## LazyZee (Nov 30, 2008)

Hello, I am coding a RPN calculator for a class using java stack. This calculator takes any infix math expression (+,-,/,*)and converts it to a postfix and evaluates the postfix form to calculate an answer. This calculator also converts mathematical expressions with parenthesis. This is why you convert the infix expression to postfix to get rid of () and have correct order of precedence. Now I pretty much have that done but it didn't occurr to me till after i finished that there is a huge flaw! I only tested if it works for single digits, but anything above a single digit will not work!


```
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stack;
import java.util.Scanner;
import java.util.Stack;
/**
 *
 * @author LazyZee
 */
public class TCalculator {
    
       Stack<Character> operatorStack;
        Stack valueStack;
       	String infix, postfix;

	Stack<Character> stk;
        
        TCalculator() {
stk = new Stack<Character>();
valueStack = new Stack<Integer>();
operatorStack = new Stack<Character>();
        }



    	public String infixToPostfix(String inf)
        {

		infix = inf;
		postfix = "";

		char s = 'a';
		for(int i = 0; i < infix.length(); i++)
		{
					s = infix.charAt(i);
			switch(s)
			{
				case '(': stk.push(s);
				break;
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
                                    postfix += String.valueOf(s) + " ";
                        	break;
				case ')': while((s = stk.pop()) != '(')
							postfix += String.valueOf(s) + " ";
                                
				break;
				case '/': while(!stk.empty() && stk.peek()=='/')
							postfix += String.valueOf(stk.pop()) + " ";
						  stk.push(s);
                                                                                          infix = infix.substring(i,infix.length());
                                                                                         i = 0;
				break;
				case '*': while(!stk.empty() && (stk.peek()=='/' || stk.peek()=='*'))
							postfix += String.valueOf(stk.pop()) + " ";
						  stk.push(s);
                                                                                          infix = infix.substring(i,infix.length());
                                                                                         i = 0;
				break;
				case '+': while(!stk.empty() && (stk.peek()=='/' || stk.peek()=='*' || stk.peek()=='+'))
							postfix += String.valueOf(stk.pop()) + " ";
						  stk.push(s);
                                                                                          infix = infix.substring(i,infix.length());
                                                                                         i = 0;
				break;
				case '-': while(!stk.empty() && (stk.peek()=='/' || stk.peek()=='*' || stk.peek()=='+' || stk.peek()=='-'))
							postfix += String.valueOf(stk.pop()) + " ";
						  stk.push(s);
                                                                                          infix = infix.substring(i,infix.length());
                                                                                         i = 0;
				break;
			}
		}

		while(!stk.empty())
			postfix += String.valueOf(stk.pop());
                System.out.println(postfix);
		return postfix;
	}

    
        public int evaluatePostfix(String postfix){

            int characterCount = postfix.length();
            int operandTwo;
            int operandOne;
            int result=0;
        
            for (int index=0; index<characterCount; index++) {   
               
                char nextCharacter = postfix.charAt(index);
                System.out.println("Here I am printing out the value stack" + valueStack) ;
                
     switch(nextCharacter) {
         case '0':
             valueStack.push(nextCharacter);
             break;
          case '1':
             valueStack.push(nextCharacter);
             break;
         case '2':
             valueStack.push(nextCharacter);

             break;
         case '3':
             valueStack.push(nextCharacter);

             break;
         case '4':
             valueStack.push(nextCharacter);
             break;
         case '5':
             valueStack.push(nextCharacter);
             break;
         case '6':
             valueStack.push(nextCharacter);
             break; 
         case '7':
             valueStack.push(nextCharacter);
             break;
         case '8':
             valueStack.push(nextCharacter);
             break;
         case '9':
             valueStack.push(nextCharacter);
             break;
         case '*':
             operandOne = Integer.parseInt(valueStack.pop().toString());
              operandTwo = Integer.parseInt(valueStack.pop().toString());
             result = operandOne * operandTwo ; 
             valueStack.push(result); 
             break ; 
         case '+':
             System.out.println(valueStack);
             operandOne = Integer.parseInt(valueStack.pop().toString());
              operandTwo = Integer.parseInt(valueStack.pop().toString());
             result = operandOne + operandTwo ; 
             valueStack.push(result); 
             break ; 
         case '/':
            operandOne = Integer.parseInt(valueStack.pop().toString());
              operandTwo = Integer.parseInt(valueStack.pop().toString());
             result = operandOne / operandTwo ; 
             valueStack.push(result); 
             break ; 
         case '-':    
             operandOne = Integer.parseInt(valueStack.pop().toString());
              operandTwo = Integer.parseInt(valueStack.pop().toString());
             result = operandOne * operandTwo ; 
             valueStack.push(result); 
             break ; 
             
         default:
             System.out.println("test");
             break  ;
             
     }
                 
        }
           return (Integer)valueStack.pop() ;  
    }
   
 public static void main(String[] args) {
     TCalculator hi = new TCalculator();
     String test;
     System.out.println("Hi welcome to calculator program...\n") ; 
     Scanner keyboard = new Scanner(System.in) ;
     System.out.println("Enter an infix expression\n") ; 
     String input = keyboard.nextLine() ; 
     test = hi.infixToPostfix(input);
     System.out.println(test);
     System.out.println(hi.evaluatePostfix(test));
     
                }
}
```
Basically the way i did it was that i read in the string expression and by using the .length() method i go through the string char by char. However the biggest issue is that if its a 2 digit number, the program would still take it char by char and thus making it a two 1 digits. Here i am stuck on how I would fix this problem... any suggestions are welcomed.

Thanks in advanced.


----------

