ah yes... i forgot...
the whole code axually looks like this....
import java.util.Stack;
import java.util.Scanner;
import java.io.*;
import javax.swing.*;
public class act2ex {
private Stack theStack;
private String input;
private String output = "";
public act2ex(String in) {
input = in;
int stackSize = input.length();
theStack = new Stack(stackSize);
}
public String doTrans() {
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);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}
while (!theStack.isEmpty()) {
output = output + theStack.pop();
}
return output;
}
public void gotOper(char opThis, int prec1) {
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 = output + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char ch){
while (!theStack.isEmpty()) {
char chx = theStack.pop();
if (chx == '(')
break;
else
output = output + chx;
}
}
public static void main(String[] args)throws IOException {
String choice=JOptionPane.showInputDialog ("Enter conversion choice: "+"\n0: Exit Program"+"\n1: Infix to Postfix"+"\n2: Postfix to Infix");
int cho=Integer.parseInt (choice);
while (choice!=null){
switch (cho){
case 0:
System.exit(0);
break;
case 1:
String input = JOptionPane.showInputDialog ("Enter infix notation:");
String output;
act2ex theTrans = new act2ex(input);
output = theTrans.doTrans();
JOptionPane.showMessageDialog (null,"Infix is: "+input+ "\nPostfix is " + output + '\n');
case 2:
input=JOptionPane.showInputDialog ("Enter postfix notation:");
output=theTrans.postfix();
JOptionPane.showMessageDialog (null, "Postfix is: "+input+"\nInfix is "+output+'\n');
}
}
}
public String postfix(){
for (int i=0; i<input.length(); i++){
char c=input.charAt(i);
if (c == '+' || c == '-' || c == '*' || c == '/'){
String b=theStack.pop();
String a=theStack.pop();
theStack.push(a+c+b);
}
else{
theStack.push(String.valueOf(c));
}
output=output+c;
}
return output;
}
class Stack {
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}