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);
     }
}

Recommended Answers

All 7 Replies

What is it not doing?

It processes number well, but not correct for operator.

what do you mean by: not correct for operator?

For example, when I type input : 8*9+7
It print out output: 89**7
But correct answer is: 89*7+

ok, can you, just with words, describe exactly what your code should be doing?

In doTrans you forgot a break.

public String doTrans()  // do translation to post-fix
{
    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; // forgot break here
            case '(':
                theStack.push(ch);
                break;
            case ')':
                gotParent(ch);
                break;
            default:
                output += ch;
                break;       
        }
    }
}
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.