Space out console text without tabs using Java

580 views Asked by At

Hey I am currently writing an expression calculator in Java. I have now got it to work but can't figure out how to space out writing in the terminal what I want is two list next to each other. I would originally of used tabs but because the arrays can grow it moves the other one. Here's what I get now:

OUTPUT BUFFER               STACK
[]                          []
[(]                         []
[(]                         [4]
[(, +]                      [4]
[(, +, (]                   [4]
[(, +, (]                   [4, 2]
[(, +, (, *]                    [4, 2]
[(, +, (, *, (]                 [4, 2]
[(, +, (, *, (]                 [4, 2, -2]
[(, +, (, *, (, -]                  [4, 2, -2]
[(, +, (, *, (, -]                  [4, 2, -2, 1]
[(, +, (, *]                    [4, 2, -2, 1, -]
[(, +]                     [4, 2, -2, 1, -, *]
[]                         [4, 2, -2, 1, -, *, +]
[*]                        [4, 2, -2, 1, -, *, +]
[*]                        [4, 2, -2, 1, -, *, +, 1.5]
[%]                        [4, 2, -2, 1, -, *, +, 1.5, *]
FINAL OUTPUT: [4, 2, -2, 1, -, *, +, 1.5, *, 2, %]
INFIX: ( 4 + ( 2 * ( -2 - 1 ) ) ) * 1.5 % 2 
POSTFIX: 4 2 -2 1 - * + 1.5 * 2 % 
TOKEN   ACTION      STACK
4      Add             [4.0]
2      Add             [4.0, 2.0]
-2     Add             [4.0, 2.0, -2.0]
1      Add             [4.0, 2.0, -2.0, 1.0]
-      Operate         [4.0, 2.0, -3.0]
*      Operate         [4.0, -6.0]
+      Operate         [-2.0]
1.5    Add             [-2.0, 1.5]
*      Operate         [-3.0]
2      Add             [-3.0, 2.0]
%      Operate         [-1.0]
ANSWER: -1.0

Heres what I would like to achieve:

OUTPUT BUFFER               STACK
[]                          []
[(]                         []
[(]                         [4]
[(, +]                      [4]
[(, +, (]                   [4]
[(, +, (]                   [4, 2]
[(, +, (, *]                [4, 2]
[(, +, (, *, (]             [4, 2]
[(, +, (, *, (]             [4, 2, -2]
[(, +, (, *, (, -]          [4, 2, -2]
[(, +, (, *, (, -]          [4, 2, -2, 1]
[(, +, (, *]                [4, 2, -2, 1, -]
[(, +]                      [4, 2, -2, 1, -, *]
[]                          [4, 2, -2, 1, -, *, +]
[*]                         [4, 2, -2, 1, -, *, +]
[*]                         [4, 2, -2, 1, -, *, +, 1.5]
[%]                         [4, 2, -2, 1, -, *, +, 1.5, *]
FINAL OUTPUT: [4, 2, -2, 1, -, *, +, 1.5, *, 2, %]
INFIX: ( 4 + ( 2 * ( -2 - 1 ) ) ) * 1.5 % 2 
POSTFIX: 4 2 -2 1 - * + 1.5 * 2 % 
TOKEN   ACTION      STACK
4      Add           [4.0]
2      Add           [4.0, 2.0]
-2     Add           [4.0, 2.0, -2.0]
1      Add           [4.0, 2.0, -2.0, 1.0]
-      Operate       [4.0, 2.0, -3.0]
*      Operate       [4.0, -6.0]
+      Operate       [-2.0]
1.5    Add           [-2.0, 1.5]
*      Operate       [-3.0]
2      Add           [-3.0, 2.0]
%      Operate       [-1.0]
ANSWER: -1.0

Heres the code I am currently using to print the stack and output buffer:

System.out.println("OUTPUT BUFFER\t\t\t\tSTACK");
ArrayList<String> out = new ArrayList<String>();
Stack<String> stack = new Stack<String>();
for(String token : inputTokens){
    System.out.print(stack+ "\t\t\t\t\t");
    System.out.println(out);
    if(isOperator(token)){
        while(!stack.empty() && isOperator(stack.peek())){
            if (checkPri(token, stack.peek())<=0 || checkPri(token, stack.peek()) < 0 ){
                out.add(stack.pop());
                //System.out.println("OUTPUT: " + out);
                continue;
            }
            break;
        }
        stack.push(token);

    }else if(token.equals("(")){
        stack.push(token);
    }else if(token.equals(")")){
        while(!stack.empty() && !stack.peek().equals("(")){
            out.add(stack.pop());
        }
        stack.pop();
    } else {
        out.add(token);
        //System.out.println("OUTPUT BUFFER: " + out);

    }
}
1

There are 1 answers

2
Valen On BEST ANSWER

you can use String.format(String,args...) or System.out.printf(String,args...);

I don't know how to explain due to my poor english, but here the example:

int i=10000,j=200,k=5000000;
System.out.printf("%10d%10d%10d",i,j,k);

will get this output:

10000     200       5000000
^         ^         ^each has ten spaces between them because of %10d