954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with a second infix to postfix and reversing infix to postfix

ok, I have a program that uses a list and stack to convert Infix to Postfix. I finally worked out all the kinks with it except one thing, which I'll get into in a minute, and now I want to reverse the process, turn Postfix into Infix. I gave it a go, and the output was all wrong. I tried debugging it and I have no idea where it's messing up. So I have 2 problems

1)If I make a MyTest class like:

public class MyTest{

	public static void main(String args[]){

		Postfix px = new Postfix();

		String input = "(a1+a2*a123)+123";
		px.parsing(input);
		px.conversion();
		px.clear();
		
		input = "a*b+c*d*e+f";
		px.parsing(input);
		px.conversion();
		px.clear();
	}
}


It will output the first part correctly, but when it gets to the second input it throws:
java.lang.StringIndexOutOfBoundsException
right after running the parsing method, which I don't understand why it would do that.

2) When I run the MyTest class without the second input but with the reverse method in there it prints out a weird output that looks like this:
( a123 + ) )( ( ( + a1 ) * a2 )( ( a1 + ( a2 * a123 ) ) + 123 ) when it should be
( ( a1 + ( a2 * a123 ) ) + 123 )
I walked through the debug and I don't see how it could be doing this because it looked ok to me, but then again I am also new to debugging.

I'll put up the code to my Postfix class below. Any help you guys could offer would be greatly appreciated.

<pre><code>class Postfix {

private ListReferenceBaseCSCE1040 token = new ListReferenceBaseCSCE1040();
private String output;
private StackReferenceBaseCSCE1040 aStack = new StackReferenceBaseCSCE1040();

public Postfix(){

token = new ListReferenceBaseCSCE1040();
aStack = new StackReferenceBaseCSCE1040();
output = &quot;&quot;;

}//end constructor postfix

//this takes token and converts it to postfix
public void conversion(){

for(int i = 1; i &lt;= token.size(); i++){

if( token.get(i).equals(&quot;(&quot;)){
aStack.push(token.get(i));
}else if(token.get(i).equals(&quot;)&quot;)){
while(!aStack.peek().equals(&quot;(&quot;)){
output = output + &quot; &quot; + aStack.pop() + &quot; &quot;;
} //end while
aStack.pop(); // to pop the ( so it doesn't get printed later
}else if((token.get(i).equals(&quot;*&quot;)) || (token.get(i).equals(&quot;/&quot;)) || (token.get(i).equals(&quot;+&quot;)) || (token.get(i).equals(&quot;-&quot;))){
while(!aStack.isEmpty() &amp;&amp; precedence(token.get(i)) &lt;= precedence(aStack.peek())){
output = (output + &quot; &quot; + aStack.pop());
} //end while
aStack.push(token.get(i));
}else{
output += token.get(i) + &quot; &quot;;
} //end ifs

} //end for

while(!aStack.isEmpty()){
output = output + aStack.pop();
} // end while

System.out.println(&quot;Output: &quot; + output);
System.out.println(&quot;&quot;);

} //end conversion

// sets the precedence of the operators
public int precedence(Object op){

if((op.equals(&quot;*&quot;)) || (op.equals(&quot;/&quot;))){
return 2;
}else if((op.equals(&quot;+&quot;)) || (op.equals(&quot;-&quot;))){
return 1;
}else{ //parenthesis
return 0;
} //end ifs

} //end precedence

public void parsing(String input){

String aToken = &quot;&quot;;
int numToken = 0;
int x = 0;

if(output == &quot;&quot;){
System.out.println(&quot;Infix to Postfix&quot;);
}

while( x &lt; input.length() ){

if((input.charAt(x) == '(') || (input.charAt(x) == ')') || (input.charAt(x) == '*') || (input.charAt(x) == '/') || (input.charAt(x) == '+') || (input.charAt(x) == '-')){
aToken = aToken + input.charAt(x);
numToken++;
token.add(numToken, aToken);
aToken = &quot;&quot;;
}else if(Character.isLetter(input.charAt(x))){
aToken += input.charAt(x);
while((x+1&lt;input.length()) &amp;&amp; (Character.isLetter(input.charAt(x+1))) || (Character.isDigit(input.charAt(x+1)))){
x++;
aToken += input.charAt(x);
} //end while
numToken++;
token.add(numToken, aToken);
aToken = &quot;&quot;;
}else if(Character.isDigit(input.charAt(x))){
aToken += input.charAt(x);
while((x+1 &lt; input.length()) &amp;&amp; (Character.isDigit(input.charAt(x+1)))){
x++;
aToken += input.charAt(x);
} // end while
numToken++;
token.add(numToken, aToken);
aToken = &quot;&quot;;
}

x++; //adds to the main while loop

} // end while
System.out.println(&quot;Input: &quot; + input);

} // end parsing


public void clear(){

token = new ListReferenceBaseCSCE1040();
aStack = new StackReferenceBaseCSCE1040();
output = &quot;&quot;;

}//end clear


//THIS IS WHERE THINGS GET WEIRD********************************************
public void reverse(){
String tempout = &quot;&quot;;
Object operand1 = &quot;&quot;;
Object operand2 = &quot;&quot;;

System.out.println(&quot;Postfix to Infix&quot;);
this.parsing(output);

for(int i = 1; i &lt; token.size(); i++){
if(token.get(i).equals(&quot;*&quot;) || token.get(i).equals(&quot;/&quot;) || token.get(i).equals(&quot;+&quot;) || token.get(i).equals(&quot;-&quot;)){
operand2 = aStack.pop();
operand1 = aStack.pop();
tempout = (&quot;( &quot; + operand1 + &quot; &quot; + token.get(i)+ &quot; &quot; + operand2 + &quot; )&quot;);
aStack.push(tempout);
tempout = &quot;&quot;;
}else{
aStack.push(token.get(i));
}//end ifs
}//end for
while(!aStack.isEmpty()){
tempout = tempout + aStack.pop();
}
System.out.println(&quot;Output: &quot; + tempout);
}//end reverse
}//end postfix</code></pre>

gramatton
Newbie Poster
5 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

scratch the second problem. I fixed my reverse method. Main problem was a forgotten <= in the for loop. I still can't figure out why its throwing that exception though so any help with that would be great.

gramatton
Newbie Poster
5 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You