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.??

31st Jul 2017, 7:31 PM
John
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
31st Jul 2017, 7:50 PM
Matte
Matte - avatar
+ 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;
31st Jul 2017, 10:12 PM
Matte
Matte - avatar
0
can you please write me an example?? i mean with coding?
31st Jul 2017, 9:59 PM
John
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); }
1st Aug 2017, 4:10 PM
John