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