0
Decimal point in Calculator
hi, I am trying to make a calculator but i am having a problem. i don't want the user to be able to enter two decimal point in the same number, for example, 2,23,4>> of course that is wrong please help me, how can i let the user write only one (comma) in his number.??
4 Réponses
+ 1
some time ago i had the same problem and i've found two solution
1 - set a boolean variable "control", when an operator (+,-,*,...) is called it is set to False and when is called a comma it is set to True, when comma is called you need first to verify that "control" is False
2 - every time that you call a comma, you verify that from that point to the first operator before there aren't any commas
+ 1
this comes after a switch on the input of the user
op(control) call a function that verifies if control is equal to one of the operators (+,-,*,/)
case ".":
boolean found = false;
int i = 1;
while (found != true) {
String control = text.substring(text.length() - i, text.length() - i + 1);
if (control.equals(".")) {
found = true;
Toastview("You can have only one dot");
break;
} else if (op(control)) {
found = true;
write(a);
break;
}
i++;
}
break;
0
can you please write me an example??
i mean with coding?
0
I made it as the following ;
but i still have a problem, how to prevent the user from starting his number with a comma ?
have you any idea please?
String text = txt.getText();
else if ( text.contains("."))
txt.setText("");
else {
String text2 = txt.getText()+ ".";
txt.setText(text2);
}