I do the program to practice what I have learned in a class at my university(convert input of infix form to post fix one), but my program doesn't run correctly. Please help my to find out my bug. ==
IntoPost class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mr. Bean
*/
package intopost;
import java.util.Stack;
public class IntoPost
{
private Stack<Character> theStack = new Stack<>();
private String input;
private String output = "";
//.........................................................................
public IntoPost(String input) // constructor
{
this.input = input;
}
//.........................................................................
public String doTrans() // do transalation to postfix
{
for ( int j=0; j < input.length(); j++)
{
char ch = input.charAt(j);
switch(ch)
{
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
case '(':
theStack.push(ch);
break;
case ')':
gotParent(ch);
break;
default:
output += ch;
break;
}
}
return output;
} //end doTran()
//.........................................................................
public void gotOper(char opThis, int prec1) // got operator from input
{
while(!theStack.isEmpty())
{
char opTop = theStack.pop();
if (opTop == '(')
{
theStack.push(opTop);
break;
}
else
{
int prec2;
if(opTop == '+' || opTop == '-')
{
prec2 = 1;
}
else
{
prec2 = 2;
}
if (prec2 < prec1)
{
theStack.push(opTop);
break;
}
else
{
output += opTop;
}
} // end else
} // end while
theStack.push(opThis);
} // end gotOp()
//.........................................................................
public void gotParent(char ch)
{
while (!theStack.isEmpty())
{
char chx = theStack.pop();
if (chx == '(')
{
break;
}
else
{
output += chx;
}
}
}
}// end class IntoPost
IntoPostTest class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mr. Bean
*/
import intopost.IntoPost;
import java.util.Scanner;
public class TestIntoPost
{
public static void main(String[] args)
{
String input;
String output;
System.out.println("Enter infix: ");
Scanner ca = new Scanner(System.in);
input = ca.nextLine();
IntoPost theTrans = new IntoPost(input);
output = theTrans.doTrans();
System.out.println(output);
}
}