Need to remove dashes ("-") from a String value provided by the user

Posted on 2008-11-17
Last Modified: 2013-11-23
Dear Fellow Java developers:

I am trying to develop a method that validates a number that is given by a user.  The number is nine characters in length, and allows the user to send it in either of the following formats:

1. xxxxxxxxx
2. xxx-xxx-xxx

The validation method needs to check to see if there are dashes ("-") within the number, and if it finds any, then it should remove all occurrenes, then check to see if the number is 9 characters in length, and then return that number back.  

Please help.
Question by:fsyed
    LVL 86

    Accepted Solution


    s = s.replaceAll("-", "").trim();
    if (s.length() == 9) {
       // valid

    Open in new window


    Author Closing Comment

    Thanks for the quick reply!
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now