I am trying to input an infix expression and convert it to a postfix expression using Stack class. I will be outputting the postfix expression as a String.
This is my code so far:
import java.util.*;
public class Fix
{
public static void main(String[]args)
{
Scanner s= new Scanner (System.in);
System.out.println("Enter expression in infix: ");
String input= s.nextLine();
Stack<String> operator= new Stack<String>();
Stack<String> operand= new Stack<String>();
System.out.println("Created stacks...");
String expression="";
transferToString(input, operator, operand, expression);
System.out.println("Created string... ");
System.out.println(expression);
}
}
Here is the statement that creates the String expression in the void transferToString method:
else if (operand.empty()==false)
{
String str1 = input.substring(i++,i+2);
expression.concat(operand.pop());
expression.concat(str1);
}
The above code is a nested if statement, with the primary condition being whether the next character is an operand or not.
In the above else-if statement, I pop an operand from the stack operand, and concatenate it to String expression. In the main method, I print out expression.
However, this is my output:
Enter expression in infix:
(1+5)
Created stacks...
Pushed first operator!
Created string...
For some reason, the String is not getting printed - I don't understand where I'm going wrong. Can somebody please point out what I'm doing wrong?
Thank you very much!