Validation method

Posted on 2005-04-20
Last Modified: 2010-03-31
I need help writing a method that determines if all the characters in a passed string are numbers.

method skeleton :
public boolean isNumeric (String field){
  boolean isValid;
  //determine if string all numbers
 //if string all numbers isValid = true;
 //else isValid = false;
 return isValid;


Please help.
Question by:apparition
    LVL 86

    Accepted Solution

    boolean valid = field.matches("\\d+");
    LVL 25

    Expert Comment

    full points to CEHJ, but this would be a more efficient method to perform this, I imagine:

    public boolean isNumeric( String field )
        return field.matched( "\\d+" );               // No idea how this works, but you're the man CEHJ
    LVL 25

    Expert Comment


         return field.matches( "\\d+" );

    LVL 86

    Expert Comment

    LVL 3

    Expert Comment

    It is all about patterns, regular expression etc.

    d indicates a digit ( 0 to 9)
    + indicates one or more.

    So, matches method returns true if the field has one or more digits and nothing else. Otherwise it will return false.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    What is the compatible hibernate version for spring 4.3.1 10 51
    count8 challlenge 13 69
    countHi challenge 25 63
    mapBully challenge 6 55
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    This video teaches viewers about errors in exception handling.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now