0

I have the following code. I am typing in the terminal 'javac Hwk2014.java' followed by 'java Hwk2014 1 2 + ' and this should give me an output of 3. When I do it with a newly generated array 'args2', I do get 3. When I type in the arguments with a for loop, it clearly says args[0] is 1 and so forth. So its all there, but the output keeps being '0'

code:

import java.util.*;

public class Hwk2014 {

    public static void main(String[] args) {
        System.out.println("Hi");
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + i + ": " + args[i]);
        }
        String[] args2 = new String[3];
        args2[0] = "1";
        args2[1] = "2";
        args2[2] = "+";
        /*args2[3] = "3";
        args2[4] = "*";*/
        //System.out.println("+args[0]");
        String value1, value2;
        int addValue = 0, multiplyValue = 0, lastValue = 0;
        //int j = 0, h = 0;
        String addValueS, multiplyValueS;
        Stack<String> object = new Stack<>();
        for(int i = 0; i < args.length; i++)
        {
            object.push(args[i]);
            if (object.size() >= 2 && object.peek() == "+")
            {
                object.pop();
                value1 = object.pop();
                value2 = object.pop();
                addValue = Integer.parseInt(value1) + Integer.parseInt(value2);
                addValueS = Integer.toString(addValue);
                object.push(addValueS);
                if (i == (args.length - 1))
                {
                    lastValue = addValue;
                }
            }
            if (object.size() >= 2 && object.peek() == "*")
            {
                object.pop();
                value1 = object.pop();
                value2 = object.pop();
                multiplyValue = Integer.parseInt(value1) * Integer.parseInt(value2);
                multiplyValueS = Integer.toString(multiplyValue);
                object.push(multiplyValueS);
                if (i == (args.length - 1))
                {
                    lastValue = multiplyValue;
                }
            }
        }
        System.out.println(lastValue);
    }
}

Test:

Input: ~%java Hwk2014 1 2 + 

output: 0
Abra
  • 19,142
  • 7
  • 29
  • 41
Sam
  • 3
  • 1

2 Answers2

0

Your code is so complicated for something that simple.

public class Main
{
    public static void main(String[] args) throws Exception
    {
        int value1 = Integer.parseInt(args[0]);
        int value2 = Integer.parseInt(args[1]);
        String modifier = args[2];
        int result;
        
        switch(modifier) {
            case "+": result=(value1 + value2); break;
            case "-": result=(value1 - value2); break;
            case "*": result=(value1 * value2); break;
            default : result=(value1 / value2); break;
        }
        
        System.out.println("result: " + result);
    }
}
$ java Main 4 2 +
result: 6
$ java Main 4 2 -
result: 2
$ java Main 4 2 \*
result: 8
$ java Main 4 2 \/
result: 2
Darkman
  • 2,941
  • 2
  • 9
  • 14
  • Your code only works for 2 numbers. The input can be more than that. EX: 1 2 + 3 * 3 + 2 – Sam Feb 18 '22 at 08:22
0

TL;DR

The below is my interpretation of your question but I may have misinterpreted it. You may simply be trying to write code to Evaluate the Value of an Arithmetic Expression in Reverse Polish Notation in Java

My Answer

The below code does not check that the [java] command-line arguments are valid. It assumes that the arguments are repeats of

number number operator

where operator is one of the following:

  • + for addition
  • / for division
  • * for multiplication
  • - for subtraction

Note that because of the way that the operator characters are interpreted on the command line, you may need to delimit them. (See example after below code.)

import java.util.Map;
import java.util.Stack;

public class Hwk2014 {
    enum Operator {
        ADD, DIVIDE, MULTIPLY, SUBTRACT;

        public String toString() {
            switch (this) {
                case ADD:
                    return "\uFF0B";
                case DIVIDE:
                    return "\u00F7";
                case MULTIPLY:
                    return "\u00D7";
                case SUBTRACT:
                    return "\u2212";
                default:
                    return this.toString();
            }
        }
    }

    private static int add(int operand1, int operand2) {
        return operand1 + operand2;
    }

    private static void calculate(Operator operator, int operand1, int operand2) {
        double result = Double.MIN_VALUE;
        switch (operator) {
            case ADD:
                result = add(operand1, operand2);
                break;
            case DIVIDE:
                result = divide(operand1, operand2);
                break;
            case MULTIPLY:
                result = multiply(operand1, operand2);
                break;
            case SUBTRACT:
                result = subtract(operand1, operand2);
                break;
            default:
                System.out.println("Unhandled operator: " + operator);
        }
        System.out.printf("%d %s %d = %f%n", operand1, operator, operand2, result);
    }

    private static double divide(int operand1, int operand2) {
        return operand1 / (double) operand2;
    }

    private static double multiply(int operand1, int operand2) {
        return operand1 * operand2 * 1.0d;
    }

    private static int subtract(int operand1, int operand2) {
        return operand1 - operand2;
    }

    public static void main(String[] args) {
        Map<String, Operator> map = Map.of("+", Operator.ADD,
                                           "/", Operator.DIVIDE,
                                           "*", Operator.MULTIPLY,
                                           "-", Operator.SUBTRACT);
        Stack<String> stack = new Stack<>();
        for (String arg : args) {
            stack.push(arg.replace("'", ""));
        }
        Operator operator = null;
        int operand1 = 0;
        int operand2 = 0;
        int remainder = -1;
        for (int i = 0; i < args.length; i++) {
            String val = stack.pop();
            remainder = i % 3;
            switch (remainder) {
                case 0:
                    if (operator != null) {
                        calculate(operator, operand1, operand2);
                    }
                    operator = map.get(val);
                    break;
                case 1:
                    operand1 = Integer.parseInt(val);
                    break;
                case 2:
                    operand2 = Integer.parseInt(val);
                    break;
                default:
                    System.out.println("Problem? remainder = "  + remainder);
            }
        }
        if (remainder == 2  &&  operator != null) {
            calculate(operator, operand1, operand2);
        }
    }
}

Here is a sample command (on Windows 10):

java Hwk2014 2 1 + 7 6 '*' 7 22 "/" 12 5 "-"

And here is the output:

5 − 12 = -7.000000
22 ÷ 7 = 3.142857
6 × 7 = 42.000000
1 + 2 = 3.000000

Refer to Enum Types+
Refer to The Map Interface+
Refer to Characters+
Refer to Formatting Numeric Print Output+

+Part of Oracle's Java tutorials

Your Code

There are two problems in your code. Here is your corrected code with explanations of the problems after the code. I changed two lines, both of which are marked with the comment CHANGES HERE

import java.util.*;

public class Hwk2014 {

    public static void main(String[] args) {
        System.out.println("Hi");
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + i + ": " + args[i]);
        }
        String[] args2 = new String[3];
        args2[0] = "1";
        args2[1] = "2";
        args2[2] = "+";
        /*args2[3] = "3";
        args2[4] = "*";*/
        //System.out.println("+args[0]");
        String value1, value2;
        int addValue = 0, multiplyValue = 0, lastValue = 0;
        //int j = 0, h = 0;
        String addValueS, multiplyValueS;
        Stack<String> object = new Stack<>();
        for(int i = 0; i < args.length; i++)
        {
            object.push(args[i]);
            if (object.size() > 2 && "+".equals(object.peek())) // CHANGES HERE
            {
                object.pop();
                value1 = object.pop();
                value2 = object.pop();
                addValue = Integer.parseInt(value1) + Integer.parseInt(value2);
                addValueS = Integer.toString(addValue);
                object.push(addValueS);
                if (i == (args.length - 1))
                {
                    lastValue = addValue;
                }
            }
            if (object.size() > 2 && "*".equals(object.peek())) // CHANGES HERE 
            {
                object.pop();
                value1 = object.pop();
                value2 = object.pop();
                multiplyValue = Integer.parseInt(value1) * Integer.parseInt(value2);
                multiplyValueS = Integer.toString(multiplyValue);
                object.push(multiplyValueS);
                if (i == (args.length - 1))
                {
                    lastValue = multiplyValue;
                }
            }
        }
        System.out.println(lastValue);
    }
}

The condition object.size() >= 2 will be true when the Stack only contains two elements but you need it to contain three elements, hence I changed it to object.size() > 2. That is your first problem.

The second problem is that you are not correctly comparing strings. You need to use method equals and not the operator ==. Refer to How do I compare strings in Java?

When I run your corrected code, I get the following output:

Hi
Argument 0: 2
Argument 1: 1
Argument 2: +
3
Abra
  • 19,142
  • 7
  • 29
  • 41
  • why is there is a need for ' ' around * and " " around - or / ? I tried java Hwk2014 1 2 + 3 * and the output was: Argument 0: 1, Argument 1: 2, Argument 2: +, Argument 3: 3, Argument 4: Hwk2014.class, Argument 5: Hwk2014.java, Argument 6: Testing.java, 0 – Sam Feb 18 '22 at 16:13
  • It works for / alone as well. Only * requires ' ' around it. Wonder why – Sam Feb 18 '22 at 16:29
  • _Only * requires ' ' around it._ Because `*` is a special character for the command interpreter you are using. On Windows this is [CMD](https://en.wikipedia.org/wiki/Cmd.exe) which replaces `*` with all the files in the working directory. Since I could not ascertain which operating system you are using, I wrote in my answer: _Note that because of the way that the operator characters are interpreted on the command line, you may need to delimit them_ – Abra Feb 19 '22 at 04:06