need help converting from infix to postfix notation
guys, I have an expression like: 1 + 2 * 3, and I need to convert to postfix notation: 1 2 3 * +
This is my code, but for some reason I'm not displaying the operators, any help appreciated
Code:
package postfixstack;
import java.io.*;
import java.util.*;
public class Main {
static Stack<String> optor = new Stack();
static Stack<Integer> oprand = new Stack();
static String strOut = "";
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));
strOut += next+" ";
}
next = in.next();
}while(!next.equals("$"));
}
System.out.println(strOut);
}
//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("here!");
strOut += optor.top()+" ";
optor.pop();
}while(true);
}
}
Re: need help converting from infix to postfix notation