• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

String only contains digits and "-"

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i make a method that makes sure that a String only contains digits and "-" (dash) ... (I want to make sure that the method can see if the user has written in an okay telephone number...)


This is what I've done this far.. But the method retuns false if you type - or +



Help much appreciated! Thanks
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
generally I use regex to do things like these. It allows you to not only check if the string is made up of numbers and dashes, but also check the string os of the format NNN-NNN-NNNN

If you don't want to use regex, you can split the string by "-" and then check each part to see if each is a number
 
Sheriff
Posts: 26777
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want something more specific than "only digits and -". For example you might not like "1-2---3---" or many other such strings. Probably you only want phone numbers correctly formatted for your country? If that's the case then I think a regex is the right way to go.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use regex of the format \d{3}-?\d{3}-?\d{4} where \d represents a numeric digit and the number in the curly braces indicate the number of digits that are possible.
for example \d{3} should contain three digits.
? (this indicates the character preceding can be ignored or can be used once). In our case -(dash) can be either ignored or can be used once.
 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
^(\d+-?)*\d+$
This will match any String which contains only digits and dashes but does not allow two dashes in a row and doesn't allow dash at the beginning and/or end of the String.

^\+?(\d+-?)*\d+$
The same as above, but also allows + sign at the beginning.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gregers Svensson wrote:I want to make sure that the method can see if the user has written in an okay telephone number...


Which is not as easy as it might seem.

The first thing is that telephone numbers in different countries have different formats, so you'd need to be very clear about what country you're dealing with.

The second is that dashes are a visual way for us puny humans to break up a telephone number; they are NOT part of the number itself. An alternative (as I believe someone else already suggested) would be to split the number itself into distinct fields without dashes, eg: country code, area code/exchange, line number.
Then you would only have to deal with actual numbers.

There is a lot to know about telephone numbers, as there is about any standardised number or code, and the one for telephone numbers is E.164 - which, as you can see, is not small.

HIH

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic