I was working on an infix to postfix program (using stacks), but something went wrong somewhere.
Please check whether my intopost technique is right because I am getting the output as infix without conversion.

    //stack class also containing the intopostfix method
   import java.util.*;
    public class Stack 
    {   int i,j;
char postfix[];
char stack[];
int top;
String post;
public Stack(int n)
{
    stack=new char[n];
    top=-1;
}
public void push(char item)
{
    if(top>=stack.length)
        System.out.println("Stack overflow");
    else
    {
        stack[++top]=item;
    }
}
public char pop()
{
    if(top==-1)
    {       System.out.println("Stack underflow");
            return 0;
    }
    else
        return stack[top--];
}
boolean isAlpha(char ch)
{
    if((ch>='a'&&ch<='z')||(ch>=0&&ch<='9'))
        return true;
    else 
        return false;

}
boolean isOperator(char ch)
{
    if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
        return true;
    else return false;

}

void intopost(String str)
{
    postfix=new char[str.length()];
    char ch;

    j=0;

    for(i=0;i<str.length();i++)
    {
        ch=str.charAt(i);
        if(ch=='(')
            push(ch);
        else if(isAlpha(ch))
        {
            postfix[j++]=ch;
        }
        else if(isOperator(ch))
        {
            push (ch);
        }
        else if(ch==')')
        {
            while((pop())!='(')
                    {
                        postfix[j++]=pop();
                    }
        }

    }

}
void disp()
{
    for(i=0;i<postfix.length;i++)
    {   
        System.out.print(postfix[i]);
    }
}
}
a2nacademy123 commented: import java.util.*; public class Stack { int i,j; char postfix[]; char stack[]; int top; String post; public Stack(int n) { stack=new +0

Update: I've found a solution from Scaler

commented: A little spammy? -4

I have looked at the suitable resources (GFG, Programiz, tutorialspoint) to get the answer but I found one resource which is added in the source makes more sense to me that's the reason added in the comment.
By the way thanks - I was able to complete my assignment on time and More thanks to DaniWeb, found a lot of resources that helped me to complete my assignment on time.
Happy Learning!

Nice that you've found a solution, but following a ready-made solution won't exactly be a teaching moment.

Back in the day, converting infix to postfix was part of an assignment I got in one of my programming classes. What I did was read the Wikipedia description of the shunting-yard algorithm ( https://en.wikipedia.org/wiki/Shunting_yard_algorithm ) and write my implementation based on that.

  • You're trying to treat individual characters of the input as tokens, which I suspect may be related to the bugs you're experiencing. Any input number or letter can be a multi-character sequence, and the way you're constructing your output you'll end up smushing multiple numbers one after another - that's a fail if your solution's output is ever checked.
  • Therefore, using char[] is a guaranteed fail. You could use String[], but that's half-assed. Don't reinvent the wheel when the Stack class exists - the right solution is to use it.
  • You certainly could split the string into tokens by hand, but that is also a solved problem. Read up on regular expressions, you can write a regex to find the tokens, or better yet - have regexes for number, text, operators etc and use those in your code accordingly, combining them for the token finder expression.
  • You're treating division and multiplication as if they have the same priority as addition and subtraction. That's a bug. If you've got addition or subtraction as your token, you should pop any operators off the stack into output until either the top of the stack is a left parenthesis or the stack is empty. If you've got division or multiplication as the current token, you should pop any division or multiplication operations off the stack into output until either the top operator is an addition, subtraction or a left parenthesis, or the stack is empty.
  • In your implementation of handling the right parenthesis, you're using pop() to check what's at the top of the stack. There's a problem with doing this - it will decrease your stack counter in the while() and then again in the assignment - you're effectively losing every other token from the stack when that happens. That's a bug.
  • After you've processed the input tokens, you're ignoring anything that is left on the stack. What you should actually do is pop those items into the output until the stack is empty.

Since the platform ate my last edit, the gist of it was that you may want to do one last thing to improve your implementation:
You're kind of already touching on it with isOperator() and its' sister methods, but you should refactor your code, moving this functionality out of your main implementation and into a dedicated class - let's call it SymbolSet - that would handle the recognition of literals, operators, variables etc. You will then likely want another class - an OperatorSet - that would wrap an array of OperatorDefinition items, specifying each operator's properties and providing a way to get that operator list as a regular expression.
This is more complicated than hardcoding the logic right into your main implementation, but the individual classes will be individually simpler, easier to read and maintain (or expand) and have less room for errors to creep in. Let's say that you wanted to extend your converter so that it can be used to convert Boolean logic expressions - you could do it without losing the original math parser functionality, by implementing your converter's constructor so that it takes a SymbolSet as an argument and then have an enum consisting of different SymbolSets that can be used for this purpose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.