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

Infix to postfix using array stacks

Hello. I'm trying to write a program that will convert a user input infix expression into its postfix form. My current code is allowing the user to input a string, but it does nothing with the string. I'm fairly new to JAVA, thus I think I'm making a simple mistake in the main method. I'm using a generic class for a stacks implemented using arrays to store any data type. Thanks in advance for any help.

import java.io.*;
import java.util.*;
//begin coding for the stack interface
interface Stack
{
public boolean isEmpty();//tests is current stack is empty. Returns true if so, and false if not.
public E top() throws StackException;//retrieves value at the top of the stack. Stack cannot be empty.
public void push(E value) throws StackException;//pushes a value on the top of the stack.
public void pop() throws StackException;//removes a value from the top of the stack. Stack cannot be empty.
}//terminates coding of Stack interface

//begin coding for the objArrayStack class
class objArrayStack implements Stack
{
//constructor
public objArrayStack()
{
topValue=-1;
}//terminates constructor
public void push(E value)throws StackException
{
if(topValue operatorStack = new objArrayStack();
//postfix string initialized as empty
String postfixString = " ";
//scan infix string and take appropriate action
for(int index = 0; index < infixString.length(); ++index)
{
char chValue = infixString.charAt(index);
if(chValue == '(')
operatorStack.push('(');
else if(chValue == ')')
{
Character oper = operatorStack.top();
while(!(oper.equals('(')) && !(operatorStack.isEmpty()))
{
postfixString += oper.charValue();
operatorStack.pop();
oper = operatorStack.top();
}//end while loop
operatorStack.pop();
}//end else if
else if(chValue == '+' || chValue == '-')
{
if(operatorStack.isEmpty()) //operatorStack is empty
operatorStack.push(chValue);
else //current operatorStack is not empty
{
Character oper = operatorStack.top();
while(!(operatorStack.isEmpty() || oper.equals(new Character('(')) || oper.equals(new Character(')'))))
{
operatorStack.pop();
postfixString += oper.charValue();
}//ends while loop
operatorStack.push(chValue);
}//end else
}//end else if
else if(chValue == '*' || chValue == '/')
{
if(operatorStack.isEmpty())
operatorStack.push(chValue);
else
{
Character oper = operatorStack.top();
while(!oper.equals(new Character('+')) && !oper.equals(new Character('-')) && !operatorStack.isEmpty())
{
operatorStack.pop();
postfixString += oper.charValue();
}//end while loop
operatorStack.push(chValue);
}//end else
}//end else if
else
postfixString += chValue;
}//end for loop
while(!operatorStack.isEmpty())
{
Character oper = operatorStack.top();
if(!oper.equals(new Character('(')))
{
operatorStack.pop();
postfixString += oper.charValue();
}//end if
}//end while
return postfixString ;
}//terminates text of InToPost method

public static void main(String[]args)
{
objArrayStack mystack = new objArrayStack();
System.out.println("Enter a string");
Scanner scan = new Scanner(System.in);
scan.nextLine();
String str = scan.nextLine();
InToPost(str);
}//terminates text of main method
}//terminates text of objArrayStack class

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 

Use System.out.println("Your message") to print debugging messages about your input string and the value of every variable. First, ensure that the string is received from the scanner. After, look in the code of the stack.

GDICommander
Posting Whiz in Training
211 posts since Jun 2008
Reputation Points: 72
Solved Threads: 26
 
scan.nextLine();
String str = scan.nextLine();


This is what causes the problem. You are already moving your scanner past the line read. Remove the first scanner.nextLine();
Read the Scanner docs here for more details.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

u need to implement the stack
then do precedence with switch cases
ill give more info once i get home, but at least show some code that you wrote not the code from your resource material form CD

notuserfriendly
Junior Poster
104 posts since Jan 2007
Reputation Points: 11
Solved Threads: 7
 
u need to implement the stack then do precedence with switch cases ill give more info once i get home, but at least show some code that you wrote not the code from your resource material form CD

I implemented the stack interface and the class containing all of the methods(pop, push, etc.) and I'm pretty sure there are no errors there. I wrote this code myself, I didn't copy it from a CD or website. Also, I know for a fact that converting from infix to postfix can be done without using a switch statement. Thanks for the reply though.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 
u need to implement the stack then do precedence with switch cases ill give more info once i get home, but at least show some code that you wrote not the code from your resource material form CD

Why is that, that you believe he copied the code ?

@OP : Read my prvious post I've underlined the problem there.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Thanks for backing me up, verruckt24. It is good to see that not everyone in this forum is so cynical and suspicious. I made some ajustments to my program and got it to work. Thanks for your help.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 

Has your query been solved, if yes then please mark the thread so, so people don't keep posting here for ages :)

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

If anyone needs help with this particular problem (converting infix to postfix and evaluating), send me a message. I have a working version of the program and I'd be glad to help.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 

Instead of offering them the program, let them learn the way you did by, putting pieces together and asking wherever he's stuck. Plus it would certainly be of more benefit to all learners if it is kept on the forum. Not that he cannot PM you asking for help, but keeping it on the forum is always better for the community.

Also if you feel you have a piece of code worth sharing you can always add to the code snippets on the forum. This way the work of programming has a larger audience and user base.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Sorry, I didn't mean that I would just give them the program. I was just saying that I have it correctly completed if anyone needs help. I respect the forum rules and would never go against the policy. I know that it is important for fellow viewers to solve their own problems by themselves with just a liitle help from the community. Sorry once again for the implications of my previous post.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 
If anyone needs help with this particular problem (converting infix to postfix and evaluating), send me a message. I have a working version of the program and I'd be glad to help.

HI!...I thinked I badly needed ur help..I'm also working w/ that problem and I want to know what's wrong w/ my codes..
Here is my code:

import java.util.*;
import java.lang.String.*;
import java.lang.Object;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class InToPre
{
private Stack operators = new Stack();
private Stack operands = new Stack();
private char[] chars = null;

public InToPre(char[] chars)
{
this.chars = chars;
}

private boolean isValid()
{
boolean isValid = true;
// characters in the string can be math symbols '+ - * / ' or be space
// or
// can be paranthises '(' or ')'
// can be a digit
for(int i =0; i

cymercutie88
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You