Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help! I don't understand InFix to PostFix conversion.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help! I don't understand InFix to PostFix conversion.

    Alright so this is the code I have so far:

    public static void main(String[] args) {
    String inFix = "2*2+2";
    String postFix = InfixToPostfix(inFix);
    System.out.println("InFix: " + inFix);
    System.out.println("PostFix: " + postFix);
    }

    public static String InfixToPostfix(String equation) {
    int priority = 0;
    String result = "";
    char temp = equation.charAt(0);
    Deque<String> stack = new ArrayDeque<>();
    stack.push(temp + "");

    for (int i = 1; i < equation.length(); i++) {
    temp = equation.charAt(i);

    if (Character.isDigit(temp)) {
    result += temp;
    }

    else {
    Character operator = stack.peek().charAt(0);
    if (operator == '*' || operator == '/')
    priority = 1;

    else
    priority = 0;


    if (priority == 1) {
    result += stack.pop();
    i--;
    }

    else {
    if (temp == '+' || temp == '-') {
    result += stack.pop();
    stack.push(temp + "");
    }
    else
    stack.push(temp + "");
    }

    }
    }

    int length = stack.size();

    for (int i = 0; i < length; i++) {
    result += stack.pop();
    }
    return result;
    }

    This seems to work fine when I DON'T multiply or divide the first set of numbers (ex: 2+22) BUT when I try to divide / multiply the first set (ex: 22+2) everything messes up.
    If I put in 22+2 the result ends up being: 222+. BUT If I put in 2+22 the result is 222+. The result should be 222*+ for both cases. What am I doing wrong!?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help! I don't understand InFix to PostFix conversion.

    Please see the announcements page for the use of code tags when posting code on the forum.
    Go through the code one step at a time to see how the result is what it is. Only after you understand what it is doing can you begin to correct it.

Similar Threads

  1. Help with infix to postfix problem.
    By bankston13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2013, 05:55 PM
  2. Infix to postfix using stacks
    By kbrahmani in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 22nd, 2013, 02:54 AM
  3. [SOLVED] need help converting from infix to postfix notation
    By mia_tech in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 27th, 2012, 03:45 PM
  4. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 10:34 AM
  5. convert infix to postfix
    By tina G in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 11th, 2010, 01:46 AM