S.P. S.P. - 1 month ago 11
Java Question

My simple calculator don't do operations fine with double number

I'm doing a basic calculator and seems that works fine, unless when I try to sum/divide... a double number like for example 5.5

Then, if I try do this: 5.5 + 1 the result that I get is 56 and not 6.5. Seems that my logic don't take the point when do the operations.

Some help will be apreciate!

This is my code:

for (int i = 0; i < inputText.getText().toString().length(); i++) {
if (Character.isDigit(inputText.getText().toString().charAt(i))) {
val += inputText.getText().toString().substring(i, i + 1);
if (i == inputText.getText().toString().length() - 1) {
vals.add(Double.parseDouble(val));
}
} else if (inputText.getText().toString().substring(i, i + 1).equals("+")) {
vals.add(Double.parseDouble(val));
signs.add("+");
val = "";
} else if (inputText.getText().toString().substring(i, i + 1).equals("-")) {
vals.add(Double.parseDouble(val));
signs.add("-");
val = "";
} else if (inputText.getText().toString().substring(i, i + 1).equals("*")) {
vals.add(Double.parseDouble(val));
signs.add("*");
val = "";
} else if (inputText.getText().toString().substring(i, i + 1).equals("/")) {
vals.add(Double.parseDouble(val));
signs.add("/");
val = "";
}
}

double result = 0;
for(int i=0; i < vals.size(); i++){
if (i == 0){
result = vals.get(i);
}
else {
String sign = signs.get(i-1);
switch (sign){
case "+":
result = vals.get(i) + result;
break;
case "-":
result = result - vals.get(i);
break;
case "/":
result = result / vals.get(i);
break;
case "*":
result = result * vals.get(i);
break;
}
}
}

1. I think that the problem is in this part of the code:

//I can check the value of vals is 5.5 here when do the first loop in for loop
ArrayList<Double> vals = new ArrayList<>();

//but here the value of val is 55 and not 5.5
val += inputText.getText().toString().substring(i, i + 1); //Then, I'm
adding in the array the 55 instead the 5.5 if (i ==
inputText.getText().toString().length() - 1) {
vals.add(Double.parseDouble(val));
}

Answer

Your error is in this condition:

if (Character.isDigit(inputText.getText().toString().charAt(i))) {
    val += inputText.getText().toString().substring(i, i + 1);
    if (i == inputText.getText().toString().length() - 1) {
        vals.add(Double.parseDouble(val));
    }

You collect all digits from your String in val, stopping at +, -, *, /. You ignore all other characters, they don't fall into any of the elses and as such just result in no operation and the loop advances. What happens with an input of 5.5+1?

  • 5 gets added to val
  • . gets ignored
  • 5 gets added to val: now it's 55
  • +: "55" is parsed to vals[0] = 55, "+" is put into signs[0]
  • 1 gets added to val, the code detects it's the end of the string, so it parses "1" as vals[1] = 1.

The rest of the code happily computes 55 + 1 and you'll never find out.

Effectivity of the code aside, you should

  1. change the condition to

    if (Character.isDigit(inputText.getText().toString().charAt(i))
        || inputText.getText().toString().charAt(i) == '.') {
    
  2. add a final else that reports any fall-through characters as warnings or errors.