guys, I need to convert from infix to postfix notation. I'm reading from a file containing the following example
input: 2 + 3 ( 4 - 3 ) * 10 $
$ = eol
output: 2 3 4 3 - 10 * +
I'm inserting operands in one stack and operators in another stack; however, I'm having trouble devising how to display the operands... here's what I got so far
public class Main { static Stack<String> optor = new Stack(); static Stack<Integer> oprand = new Stack(); public static void main(String[] args) throws FileNotFoundException { File inFile = new File("datafile.txt"); Scanner in = new Scanner(inFile); while(in.hasNext()) { String next = in.next(); do { if(optor.isOperator(next)) { //process operator processOptor(next); } else { oprand.push(Integer.parseInt(next)); System.out.print(next); } next = in.next(); }while(!next.equals("$")); } } //processing operators public static void processOptor(String op) { do { if(optor.isEmpty()) { optor.push(op); return; } if(optor.priority(op) > optor.priority(optor.top())) { optor.push(op); return; } if(optor.priority(op) == optor.priority(optor.top())) { if(optor.associativity(optor.priority(op)).equals("right")) { optor.push(op); return; } } System.out.println(optor.top()); optor.pop(); }while(true); } }