• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I avoid multiple dots in calculator  RSS feed

 
ian bilgaen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment the user can type 1.224.1.2 (unlimited dots) while I want the user to use not more than one dot, after or before an operator (+ / * -)
like ex: 1.2 + 0.3 / 0.4 .



At the moment I only made it so that the dot cant be placed before or after an operator. Some suggest I use regex but how do I apply such thing? Thanks.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should really be parsing the text character by character as the user types it in, and not accept a character that would be illegal at that position.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
User can also probably use 12 .+ 03 /. 04 which is before and after operators, and used only once in each place. It seems that satisfies the requirement. Is that fine? I have added also 04 as a number, is it legal?
 
ian bilgaen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I use regex here and how do I implement it? Do I need to place it in all my onClick methods?
 
Paul Clapham
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:User can also probably use 12 .+ 03 /. 04 which is before and after operators, and used only once in each place.


You seem to be assuming that Ian's calculator has an ordinary text box where the user can type an arbitrary expression to be evaluated. So far in all of Ian's threads, I don't think we've been told what kind of component is being used. Or even how it's being used. Other calculator apps don't do that so I wouldn't assume anything about Ian's app.

All we've been asked is how to fix various proposed solutions to problems which arise from decisions about how to validate the input. It's very likely that many of the solutions being worked on weren't the right thing to do in the first place.



 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see your point Paul. What I wanted to point out to Ian, was plainly based on his first sentence:
At the moment the user can type 1.224.1.2 (unlimited dots) while I want the user to use not more than one dot...

Just wanted to let him be aware, that problem might not only be the amount of dots, but also the place of dots. If requirement is to have one dot, 12412. based on that is a legal input, while in my opinion it isn't. But you are right, I know little to nothing about his project, so some of my assumptions probably were off.
 
Paul Clapham
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ian bilgaen wrote:Can I use regex here and how do I implement it? Do I need to place it in all my onClick methods?


It's impossible to answer that without knowing what you are trying to validate. So far you have not told us that.

However if you are trying to validate an arithmetic expression which is to be evaluated, then regex is going to be difficult. If the expression follows a recursive grammar which can contain (brackets) -- which you haven't told us that -- then you can't make regex work.
 
ian bilgaen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It probably won't work because it needs real time validation. I basically have a onclicklistener on all my buttons for my calculator. This is just the piece of code for my dot.
 
Paul Clapham
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're allowing the user to type a formula, then yes, checking its validity at every keystroke is not necessarily a good approach. After the user types "42*" it looks like an invalid formula, but then you don't know what the user is going to type next.

However it's not the wrong thing to do. In fact you could check the formula's validity at every keystroke and make the "Evaluate" button enabled or not enabled based on whether the formula is valid or not. You could also change the colour of the text when the formula becomes invalid. But if you were thinking of popping up a dialog box every time the formula became invalid, then that would be the wrong thing to do.
 
Paul Clapham
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:checking its validity at every keystroke


And, if that is really what you are trying to do then your onClickListeners should all do the same thing, namely check the validity of the data.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!