Java Number Parse isNumber(final String str)

Here you can find the source of isNumber(final String str)

Description

Checks whether the provided string is a number.

License

Open Source License

Parameter

Parameter Description
str The string.

Return

True if the provided string is a number, false otherwise.

Declaration

public static boolean isNumber(final String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2013, Embraer S.A., Budapest University of Technology and Economics
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *
 * Contributors: /* w  w  w  .  j  a  va  2 s .c o m*/
 *     Robert Doczi, Akos Horvath - initial API and implementation 
 *******************************************************************************/

import java.text.NumberFormat;
import java.text.ParsePosition;

public class Main {
    /**
     * Checks whether the provided string is a number.
     * @param str The string.
     * @return True if the provided string is a number, false otherwise.
     */
    public static boolean isNumber(final String str) {
        if (isNullOrEmpty(str)) // empty string and null is not a number
            return false;
        final NumberFormat formatter = NumberFormat.getInstance();
        final ParsePosition pos = new ParsePosition(0);
        formatter.parse(str, pos);
        return str.length() == pos.getIndex();
    }

    /**
     * Checks whether the provided string is null or empty.
     * @param s The string.
     * @return True if the string is null or empty, false otherwise.
     */
    public static boolean isNullOrEmpty(final String s) {
        return s == null || s.isEmpty();
    }
}

Related

  1. isNumber(String str)
  2. isNumeric(Class cls)
  3. isNumeric(final String str)
  4. isNumeric(String str)