0

I am currently building a Reverse Polish Notation Calculator in Java. I have written code so that that when "d" is entered, it prints the numbers on the stack. However, the numbers printed are then unusable for further calculations going forward (See image below).

enter image description here

Whereas, I want the numbers, once printed, to then be usable on the command line, so that I could do the following calculation.

This is my code for that particular part of the calculator so far:

import java.io.*;
import java.util.Arrays;
import java.util.Stack;

public class SRPN {

private Stack<Integer> stack = new Stack<>();

public void processCommand(String input) {
    if(input.equals("+")) {
        long n1 = stack.pop();
        long n2 = stack.pop();
        long result = n1 + n2;

        if(result > Integer.MAX_VALUE) {
            result = Integer.MAX_VALUE;
        }
        else if(result < Integer.MIN_VALUE) {
            result = Integer.MIN_VALUE;
        }

        stack.push((int)result);
    }

    else if (input.equals("-")) {
        long n1 = stack.pop();
        long n2 = stack.pop();
        long result = n2 - n1;

        if(result > Integer.MAX_VALUE) {
            result = Integer.MAX_VALUE;
        }
        else if(result < Integer.MIN_VALUE) {
            result = Integer.MIN_VALUE;
        }

        stack.push((int)result);
    }

    else if (input.equals("*")) {
        int n1 = stack.pop();
        int n2 = stack.pop();
        int result = n1 * n2;

        if(result > Integer.MAX_VALUE) {
            result = Integer.MAX_VALUE;
        }
        else if(result < Integer.MIN_VALUE) {
            result = Integer.MIN_VALUE;
        }

        stack.push((int)result);
    }

    else if (input.equals("%")) {
        int n1 = stack.pop();
        int n2 = stack.pop();
        int result = n1 % n2;

        stack.push((int)result);
    }

    else if (input.equals("/")) {
        double n1 = stack.pop();
        double n2 = stack.pop();
        double result = n2 / n1;

        stack.push((int)result);
    }

    else if (input.equals("d")) {

        String values = Arrays.toString(stack.toArray());
        System.out.println(values);

    }

    else if (input.contentEquals("=")) {
        System.out.println(stack.peek());
    }

    else // assume it's a number
    {
        stack.push(Integer.valueOf(input));
    }
}

I just cant work out how you get the printed stack numbers to be usable.

Expected output would be that d prints the numbers entered to the stack:

1234 2345 3456 d 1234 2345 3456 d + 1234 5801 d + 7035

(as you can see above, d prints the first three entered numbers, then d + displays the 1234, adds the last two numbers of the stack, 2345 and 3456 together to get 5801, the next d + then adds 1234 and 5801 to get 7035)

Appreciate any help / tips, thank you!

enter image description here

Ben Craig
  • 77
  • 7
  • 1
    Please provide the expected output – Lino Mar 26 '19 at 07:48
  • 1
    you already have stack object, which you can use for calculations. – IfOnly Mar 26 '19 at 07:49
  • 1
    what type of the stack variable is? – Michel_T. Mar 26 '19 at 07:49
  • 2
    You need to show more code and describe what the problem is exactly. Why does printing the stack make it unusuable? – Thilo Mar 26 '19 at 07:50
  • 1
    Unfortunately, you did not describe what "unusable" means in the context of your application. But this seems to be essential, and without explaining that, we can't help you. – Seelenvirtuose Mar 26 '19 at 07:52
  • 1
    Provide a bit more code and explain it further. For now why don't you use the stack object? I mean you are just printing the value but they are stored in the stack you can perform calculation easily. – Zain Arshad Mar 26 '19 at 07:59
  • Hi guys, apologies for this, more content added now. – Ben Craig Mar 26 '19 at 08:01
  • 1
    Are you talking about input and output behavior on the command line when you say "unusable"? – CryptoFool Mar 26 '19 at 08:06
  • Hi @Steve, yes I believe so. I want the input to be printed so entering 1234 2345 3456 prints 1234 2345 3456, so that once it is printed, I could + and it would add the printed output together – Ben Craig Mar 26 '19 at 08:09
  • 1
    Still not sure I understand. You want this to all happen on the same line? So you want to end up with a single line in your terminal that looks like this: "1234 2345 3456 d 1234 2345 3456 d + 1234 5801 d + 7035" ? – CryptoFool Mar 26 '19 at 08:14
  • Hi @Steve, sorry i am explaining this badly. Please see the bottom of the original post, it shows what I expected to happen on the command line. – Ben Craig Mar 26 '19 at 08:17
  • 1
    Is my answer what you're looking for. Seems pretty simple, but that's what I think you're asking for. – CryptoFool Mar 26 '19 at 08:21

1 Answers1

2

I think you're just saying that instead of doign this:

System.out.println(values)

you want to print each number on its own line. If so, you just do this:

for n in values:
    System.out.println(n)

So instead of printing:

[1234, 2345, 3456]

you'll print:

1234
2345
3456
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Where does n come from? – Ben Craig Mar 26 '19 at 08:24
  • 1
    From 'values'. That's how 'for/in' works. This construct says to loop once for each value in 'values', setting 'n' to each value in turn. So the first time through, 'n' will be 1234, the second time 2345, the third time 3456. – CryptoFool Mar 26 '19 at 08:24