Tarikh Chouhan February 2016

How to regularly update an an integer

So I have a String[] which contains math expressions like "3+6=" or "5+9/8=". I have a function that gets the array, loops through the array and while looping, it splits the array so that I have one String[] containing the math operations like +,-,*,/ and another int[] containing the numbers in the question.

The function then puts the the answer into a int[] called answers. My question is that why when I output the answers[0], I can only see the first number and not the answer to the first question?

EDIT = I have found out that where the problem is happening. It is happening in my evaluation method. I dont know why:

public int evaluateAnswerTwoOperations(String operation, int numberOne, int numberTwo) {

    switch(operation){

        case "+":
            Log.d("usingsubtraction","plus2nmbers");
            return numberOne + numberTwo;
        case "-":
            Log.d("usingsubtraction","usingsubtraction");
            return  numberOne - numberTwo;
        case "/":
            Log.d("usingsubtraction","usingdivision");
            return numberOne / numberTwo;
        default:
            Log.d("usingsubtraction","usingmultiplication");
            return numberOne*numberTwo;

    }

Answers


Al Lelopath February 2016

Something like this:

import android.util.Log;

public class TwoNumberOperation {

    private final String PLUS = "+";
    private final String MINUS = "-";
    private final String MULTIPLY = "*";
    private final String DIVIDE = "/";
    private final String EQUAL = "=";

    private String operation;
    private int numberOne;
    private int numberTwo;

    public TwoNumberOperation(String operation, int numberOne, int numberTwo) {
        if (operation.equalsIgnoreCase(PLUS) ||
                operation.equalsIgnoreCase(MINUS) ||
                operation.equalsIgnoreCase(MULTIPLY) ||
                operation.equalsIgnoreCase(DIVIDE)) {

            this.operation = operation;
            this.numberOne = numberOne;
            this.numberTwo = numberTwo;
        }
        else {
            Log.d("error", "error: invalid operation");
            this.operation = "";
            this.numberOne = 0;
            this.numberTwo = 0;
        }
    }

    public int getAnswer() {

        switch (operation) {

            case PLUS:
                Log.d("addition", "addition");
                return numberOne + numberTwo;
            case MINUS:
                Log.d("subtraction", "subtraction");
                return numberOne - numberTwo;
            case MULTIPLY:
                Log.d("multiplication", "multiplication");
                return numberOne / numberTwo;
            case DIVIDE:
                Log.d("division", "division");
                return numberOne / numberTwo; // note the returning an int
            default:
                Log.d("error", "error");
                return -999999999;
        }
    }

    @Override
    public String toString() {
        return numberOne + operation + numberTwo + EQUAL;
    }

}

Post Status

Asked in February 2016
Viewed 3,985 times
Voted 6
Answered 1 times

Search




Leave an answer