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 5 of 5

Thread: [Help]Reverse Polish Notation

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question [Help]Reverse Polish Notation

    Hello, I've been working on this for weeks now and I'm still not getting it. It works fine with two numbers but any more than that and it'll crash or return the wrong value. Please check my code and let me know what I can do to fix it. I can only use stacks and queues. I also have a menu and driver but I don't know if that's needed. My assignment is basically to use scanner to look for input values and operators and then solve it. It's only single digits and I'm supposed to be able to input as many digits and operators as I want and the code should still be able to solve it. Please help!

    public class ReversePolishNotation
    {
        Queue <String> myQueue;
        public ReversePolishNotation()
        {
            myQueue = new LinkedList <String>();
        }
     
        public void doMath()
        {
            Stack <Integer> myStack = new Stack <Integer>();
            Queue <String> sign = new LinkedList <String>();
            Scanner console = new Scanner(System.in);
            int second;
            int first;
     
            while(console.hasNextInt())
            {
                myStack.push(console.nextInt());
            }
            while(console.hasNextLine() && !(console.nextLine()).equals("q"))
            {
                sign.add(console.nextLine());
            }
     
            String operator = sign.remove();
            second = myStack.pop();
            first = myStack.pop();
            do
            {
                if(operator.equals("+"))
                {
                    int result = second+first;
                    myStack.push(result);
                    myQueue.add(first + "+" + second + "=" + result);
                    if(!sign.isEmpty())
                    {
                        operator = sign.remove();
                        second = myStack.pop();
                        first = myStack.pop();
                    }
                }
                else if(operator.equals("-"))
                {
                    int result = first-second;
                    myStack.push(result);
                    myQueue.add(first + "-" + second + "=" + result);
                    if(!sign.isEmpty())
                    {
                        operator = sign.remove();
                        second = myStack.pop();
                        first = myStack.pop();
                    }
                }
                else if(operator.equals("*"))
                {
                    int result = second*first;
                    myStack.push(result);
                    myQueue.add(first + "*" + second + "=" + result);
                    if(!sign.isEmpty())
                    {
                        operator = sign.remove();
                        second = myStack.pop();
                        first = myStack.pop();
                    }
                }
                else
                {
                    int result = first/second;
                    myStack.push(result);
                    myQueue.add(first + "/" + second + "=" + result);
                    if(!sign.isEmpty())
                    {
                        operator = sign.remove();
                        second = myStack.pop();
                        first = myStack.pop();
                    }
                }
            }
            while(!sign.isEmpty());
        }
    }
    Last edited by Cluelesscoder; May 20th, 2014 at 10:21 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reverse Polish Notation

    What is this code supposed to do? What Exception does it throw? For what input? What line is the Exception on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse Polish Notation

    Quote Originally Posted by KevinWorkman View Post
    What is this code supposed to do? What Exception does it throw? For what input? What line is the Exception on?
    It's supposed to solve math problems postfix. So if I input 9 5 -, it'll return 4. If i input 8 4 7 + -, it'll return -3. There aren't any exceptions, not that I know of. It's just not working with any more than 2 numbers. I tried to find the problem earlier and apparently, it's not taking in all of the values that I'm inputting. the Queue only takes in every other line, I think.

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reverse Polish Notation

    Okay, so I've been working on the code and I realized a huge problem is that sign and myStack aren't taking in the values properly. Can someone please fix it. Write a short code that let's sign and myStack take in numbers and operators correctly.

    Also, for clarification, sign is supposed to take in the plus and minus signs while myStack is supposed to take in the numbers.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: [Help]Reverse Polish Notation

    No, we won't fix it for you or write code to order.

    Continue your debugging to determine why the tokens aren't being 'stacked' properly.

Similar Threads

  1. Replies: 9
    Last Post: February 26th, 2013, 11:36 AM
  2. Scientific exponential notation
    By JavaCup in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2012, 12:39 AM
  3. Reverse Polish Notation Calculator
    By captainawesome in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2012, 06:48 PM
  4. Big-O Notation question
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: November 28th, 2011, 09:52 PM
  5. [SOLVED] EVALUATION OF POSTFIX NOTATION
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 7th, 2011, 05:24 PM

Tags for this Thread