Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to detect if a string contains more than one decimal?

 
Ranch Hand
Posts: 176
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, so I was searching online for a method that only allows the textfield to contain numerals and I found this on stackoverflow which worked like a dream:



However I needed something that also allows "." for decimals and I came up with this:


Where then my second issue came up and that was I should only have one decimal. If a user enters a second decimal, I have found "re{ n} -> Matches exactly n number of occurrences of the preceding expression." on tutorialspoint.com but I have no idea how to use it

 
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that a correct way to deal with an error of input?
 
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Bartender
Posts: 4109
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or maintain a boolean 'containsDot', and refuse a dot when that boolean = true. (and vice versa).
 
Yosuf Ibrahim
Ranch Hand
Posts: 176
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Is that a correct way to deal with an error of input?



What is wrong with this method? Isn't it better to delete the wrong input from the start rather than error messages that tell the user to fix his errors??
 
Yosuf Ibrahim
Ranch Hand
Posts: 176
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:



Thank you Carey very very much. I really have no idea if not for you and everyone else on here what would have I done. And i did not use "replaceAll". Hope this is a better way..

 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better, but your error handling assumes that the incorrect character is the last one. Characters can be inserted in the middle of a string. My feeling is that a totally separate method isValid(String s) should be available to the user interface code so that the string can be checked before changed() is even called. In this way the UI can decide how to respond to error conditions.
 
Marshal
Posts: 25969
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Better, but your error handling assumes that the incorrect character is the last one. Characters can be inserted in the middle of a string.



And it's possible for the user to use copy-and-paste to insert more than one character at once. So yeah, even assuming that only one character is inserted is faulty.
 
Yosuf Ibrahim
Ranch Hand
Posts: 176
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Carey Brown wrote:Better, but your error handling assumes that the incorrect character is the last one. Characters can be inserted in the middle of a string.



And it's possible for the user to use copy-and-paste to insert more than one character at once. So yeah, even assuming that only one character is inserted is faulty.



Crap. I did not think of that. Well I guess what I shall do is have a boolean that would set to false if the criteria is not met and while it is false the color of the the textbox outline will be red and disable the button that would allow the user to continue. Safer I guess
 
Piet Souris
Bartender
Posts: 4109
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my reply I hinted to using a special DocumentFilter, that lets you inspect upfront the changes in spe, and that enables you to reject certain changes. Its use is more complicated than you have now, though.
 
Saloon Keeper
Posts: 22678
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Note that "." is a magic character in regexes, thus the backslash escape. And since backslash is a magic character in String literals, we need to escape it as well.
 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Tim Holloway
Saloon Keeper
Posts: 22678
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:int decimalCount = numberString.split("\\.").length;

Edit: No, that won't work either.



Why not?
 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Tim Holloway
Saloon Keeper
Posts: 22678
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. I didn't consider strings that were blatantly non-numeric. But I had thought that a consonstruct such as ".."  would return 0-element arrays. It would appear not. And, of course, I short-circuited on "a.b" splitting into TWO and instead counted the split character instances, which was just plain wrong.

Oh well.

In any event, I don't usually count decimals. I look for valid numbers. Like a regex match for "^[\+\-]{0:1}:digit:+(\.:digit:+){0:1}$", more or less. YMMV depending on allowable number formats (e.g., "E" notation), regex dialects and corrections for my usual bllopers, er, bloopers.
 
Paul Clapham
Marshal
Posts: 25969
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:In my reply I hinted to using a special DocumentFilter, that lets you inspect upfront the changes in spe, and that enables you to reject certain changes. Its use is more complicated than you have now, though.



But -- we haven't been told this but I think it's true -- the question is about a JavaFX text field.

So there are two issues: you need something which allows you to see what the text field would contain after the proposed input, and you need something to tell whether that's a valid value.

For the second issue: the idea of using a regex is (as we can see from this thread) fraught with difficulties. And anyway is the real requirement "Must not contain more than one decimal" or was it originally "Must represent a decimal number"? If it's the latter maybe it's easier to just try parsing it to Double and see if an exception is thrown.

I found several better ways online to validate a JavaFX text field. There's this one for example: https://gist.github.com/ricemery/4534910 which validates it to be an integer. Probably it could be easily modified to validate it as a decimal number.
 
Yosuf Ibrahim
Ranch Hand
Posts: 176
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually guys after testing this it does work as I need it to:



I thought what it does is it removes the last character if the criteria is not met, but what it actually does is it keeps removing characters from the end of the string until the criteria of only digits and one decimal point is met
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic