TheManual 0 Newbie Poster

I have managed to change an input String expression with parentheses to a postfix expression. I want to print to print the values in the stack but can't work it out. Can anyone tell me how I do this?

class Converter{                  

	private Stack converterStack;
	private String input;
	private String output = "";
 
	// Constructor for the Converter class
	public Converter(String userEntered){ 
		input = userEntered;
		converterStack = new Stack();
}//end of Converter class
	
	//Class to set stack values and define methods
	public class Stack {
			 
			private char[] theArray;
			private int topStack;
		 
			// Constructor for the Stack
			public Stack(){
				//Set to 100 values
				theArray = new char[100];
				topStack = 0;
		   }
		 
			// Stack Methods
			// Puts item on top of stack
			public void push(char i) {  
				theArray[++topStack] = i; }
		   
			// Takes an item from the top of stack
			public char pop(){         
				return theArray[topStack--]; }
		   
			// Shows item at the top of the stack
			public char peek(){        
				return theArray[topStack]; }
		 
			// Returns true if stack is empty
			public boolean isEmpty(){  
				return topStack == 0; }
	}

public String Order(){ 
     
	for(int i=0; i<input.length(); i++){
		char character = input.charAt(i);            
         
		switch(character){
			// If add or minus, go to operand method and establish appropriate precedence
    		case '+':              
    		case '-':
    			operand(character, 1);  
    			break;  
    			// If multiply or divide, go to the operand method and establish appropriate precedence
    		case '*':  
    		case '/':           
    			operand(character, 2); 
    			break;    
    			// If open bracket, push to stack
    		case '(':              
    			converterStack.push(character);   
    			break;
    			// If ), go to the parentheses method
    		case ')':              
    			parentheses(character);          
        		break;
        		//Check for Spaces
    		case ' ':     
    			break;
    			//Check for tabs
    		case '\t':
    			break;
    		default:              
    			output = output + character;
    			break;
		}
	}
	while(!converterStack.isEmpty()){
		output = output + converterStack.pop();
	}         
	return output; // Return final expression to screen         
}//end of Order class
 
// Takes the precedence of the operands into account
public void operand(char character2, int precedence1){
	
	while (!converterStack.isEmpty()) {
		char character1 = converterStack.pop();
		if (character1 == '(') { // Checks for left parentheses
			converterStack.push(character1);
			break;
		}
		else {
			int precedence2;
			if (character1 == '+' || character1 == '-')
				precedence2 = 1;
			else // No need to repeat for the operator operands
				precedence2 = 2;
			if (precedence2 < precedence1){
				converterStack.push(character1);
				break;
			}else
				output = output + character1;
			}
    	}
    converterStack.push(character2);
}//end of operand class

// Evaluates the  parentheses
public void parentheses(char character4){
	while (!converterStack.isEmpty()) {
		char character3 = converterStack.pop();
		if (character3 == '(')
			break;
		else
			output = output + character3;
    	}
	}//end of parentheses class
}
import java.io.IOException;
import java.util.Scanner;

//Main class
class Calculator{
	public static void main(String[] args) throws IOException{
		String input, output;{
       
			Scanner sc = new Scanner(System.in);
			System.out.println("Please enter an infix expression: ");
			input = sc.nextLine();
			Converter docalculation = new Converter(input);
			output = docalculation.Order();
			System.out.println("The postfix expression is: " + output); 
		}
	}  
}//end of Main class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.