Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Textbox not showing correct output

  1. #1
    bllnsr is offline Member
    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Default Textbox not showing correct output

    Hello everyone, I was looking for linked-list based stack examples on google and I found this: Infix to Postfix
    It is an infix to postfix evaluator. So I made a GUI for it but I am facing a problem when I give it an input from the textfield it returns the same string back to me and it runs fine without the GUI.
    Here is the code from that link:
    Java Code:
    package infix.to.postfix.calculator;
    import java.util.*;
    
    
    public class InfixToPostfix {
    	
    	 /**
        * The string representing this postfix expression.
        */
       private String exp;
       public String os = "";
    
       /**
        * Creates a postfix expression from the given prefix expression.
        * This version does not handle errors and does not allow
        * integer literals.
        *
        * @param infix a legal infix expression
        */
       
       public String inputStream;
       public InfixToPostfix(String infix)
       {
    	   
    	   inputStream = infix;
           // postfix output starts out empty
           exp = "";
    
           // create stack of pending operators
           Stack<String> pendingOps = new Stack<String>();
    
           // set up scanner to break up input expression
           Scanner scan = new Scanner(inputStream);
           
    
           while (scan.hasNext())
               {
                   String token = scan.next();
    
                   if (!isOperator(token))
                       {
                           // literals and variables go straight to output
                           exp = exp + token + " ";
                       }
                   else if (token.equals("("))
                       {
                           // opening parentheses are always pushed
                           pendingOps.push(token);
                       }
                   else if (token.equals(")"))
                       {
                           // pop off stack until get to matching open paren
                           while (!pendingOps.peek().equals("("))
                               {
                                   exp = exp + pendingOps.pop() + " ";
                               }
                           
                           // pop off opening parenthesis
                           pendingOps.pop();
                       }
                   else
                       {
                           // pop >= precedence operators off stack
                           // and add to output (note that we consider open paren
                           // to have low precedence here so it is not popped
                           // until the matching close paren is found)
                           // (also note that precedes handles the differences
                           // between left- and right-associative operators
                           // [left pop >= precedence; right pop only > prec])
    
                           while (pendingOps.size() > 0
                                  && precedes(pendingOps.peek(), token))
                               {
                                   if (!pendingOps.peek().equals("("))
                                       {
                                           exp = exp + pendingOps.pop() + " ";
                                       }
                                   else
                                       {
                                           // opening parenthesis is discarded
                                           pendingOps.pop();
                                       }
                               }
                           
                           // push operator on stack (except if its a close paren)
                           if (!token.equals(")"))
                               {
                                   pendingOps.push(token);
                               }
                       }
                   
               }
           
           // pop any remaining operators off the stack and add them to the output
           // (at this point if there is an open paren left on the stack that
           // means the infix formula was invalid)
           while (pendingOps.size() > 0)
               {
                   exp = exp + pendingOps.pop() + " ";
               } 
           
           System.out.println(toString());
       }
    
       /**
        * Precedence levels for arithmetic operators and parentheses.
        * Levels are spaced out to make it easier to add operators with
        * in between precedences.
        */
       private static final int ADDITIVE = 0;
       private static final int MULTIPLICATIVE = 10;
       private static final int PARENTHESES = -10;
       private static final int EXPONENTIATION = 20;
    
       /**
        * Constants for associativity.  Left associate operators group
        * left-to-right -- - is left-associative, so 3-2-1 is equivalent
        * to (3-2)-1 and not 3-(2-1); ^ (exponentiation) is
        * right-associative, so 2^3^2 is 512, not 64.
        */
       private static final int RIGHT_ASSOCIATIVE = 0;
       private static final int LEFT_ASSOCIATIVE = 1;
    
       /**
        * Returns the precedence of the given operator.
        *
        * @param op an operator or parenthesis
        * @return the precedence of the given operator.
        */
        private static int getAssociativity(String op)
       {
           switch (op)
               {
               case "^":
                   return RIGHT_ASSOCIATIVE;
    
               case "+":
               case "-":
               case "*":
               case "/":
                   return LEFT_ASSOCIATIVE;
    
               default:
                   throw new IllegalArgumentException();
               }
       }    
    
       /**
        * Returns whether the left operator should precede the right
        * operator.  The left should precede the right if it has
        * higher precedence or equals precedence and the operators are
        * left-associative.
        *
        * @param left a string giving the left operator
        * @param right a string giving the right operator
        */
       private static boolean precedes(String left, String right)
       {
           return (getPrecedence(left) > getPrecedence(right)
                   || (getPrecedence(left) == getPrecedence(right)
                       && getAssociativity(left) == LEFT_ASSOCIATIVE));
       }
    
       /**
        * Returns the precedence of the given operator.
        *
        * @param op an operator or parenthesis
        * @return the precedence of the given operator.
        */
       private static int getPrecedence(String op)
       {
           switch (op)
               {
               case "+":
               case "-":
                   return ADDITIVE;
               
               case "*":
               case "/":
                   return MULTIPLICATIVE;
    
               case "^":
                   return EXPONENTIATION;
    
               case "(":
               case ")":
                   return PARENTHESES;
    
               default:
                   throw new IllegalArgumentException();
               }
       }    
    
       /**
        * Determines if the given string is an identifier.
        *
        * @param id a string
        * @return true iff the given string is an identifier
        */
    
       private static boolean isIdentifier(String id)
       {
           return Character.isJavaIdentifierStart(id.charAt(0));
       }
    
       /**
        * Determines if the given string is an integer literal.
        *
        * @param num a string
        * @return true iff that string is an integer literal
        */
    
       private static boolean isLiteral(String num)
       {
           return (num.charAt(0) >= '0' && num.charAt(0) <= '9');
       }
    
       /**
        * Determines if the given string is an operator.
        *
        * @param op a string
        * @return true iff that string is an operator
        */
       public static boolean isOperator(String op)
       {
           return (op.length() == 1 && "()*-/+^".indexOf(op.charAt(0)) != -1);
       }
    
       /**
        * Returns a string representation of this expression.
        *
        * @return a string representation of this expression.
        */
       public String toString()
       {
           return exp;
       }
    GUI output:
    Textbox not showing correct output-calc.png
    Console Output:
    Textbox not showing correct output-calc2.png

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Textbox not showing correct output

    You really should try to post an MCVE.

    Without one, the best we can do is tell you to debug the program. What happens when you step through this with a debugger? What happens when you just add a bunch of print statements? What happens when you step through it with a piece of paper and a pencil? The goal is to figure out when the program's execution differs from your expectations.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Textbox not showing correct output

    I don't know what the problem is since you haven't included your GUI code. But that implementation is not one to be copied. The author should not have done all that processing in the constructor. And it is not necessarily using a linked list either (the implementation details of the Stack class are not part of the API).

    Why not write you own infix to postfix converter. It is much more rewarding and not that difficult.

    Regards,
    Jim
    KevinWorkman likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Not getting correct output.
    By sithclone3 in forum New To Java
    Replies: 33
    Last Post: 06-29-2014, 08:29 PM
  2. Getting correct output
    By WarmRegards in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 04:41 PM
  3. Is this the correct Output?
    By Teny in forum New To Java
    Replies: 17
    Last Post: 04-13-2009, 12:52 PM
  4. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 12:22 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •