[Solved] Calculator without using more than 1 variable [closed]


It doesn’t look like you’re properly saving the results of your string split.

x.split(" ");

returns a String[] with each of the pieces that were separated by ” ” e.g.

String x = "1 x 2";
String[] split = x.split(" ");

split[0] == "1";
split[1] == "x";
split[2] == "2";

You could probably benefit from using more descriptive names to your methods and variables.

Something like this would probably work:

public class Calculator {
    public static int result;

    public static void main(String[] args)
    {
        String expression = args[0];
        String[] terms = expression.split(" ");
        int firstNumber = Integer.parseInt(terms[0]);
        int secondNumber = Integer.parseInt(terms[2]);
        String operator = terms[1];

        switch (operator)
        {
            case "*":
                //multiply them
                break;
            case "+":
                //add them
                break;
            case "-":
                //subtract them
                break;
            case "https://stackoverflow.com/":
                //divide them
                break;
        }
    }
}

solved Calculator without using more than 1 variable [closed]