Java Integer Create toInt(String value)

Here you can find the source of toInt(String value)

Description

Extends the Integer#valueOf(String) method, in removing the leading and trailing whitespace.

License

Open Source License

Parameter

Parameter Description
value A string representing an integer value.

Exception

Parameter Description
NumberFormatException if the given value is null, or if thestring can't be parsed as an integer value.

Return

An integer representation of the given string.

Declaration

public static int toInt(String value) throws NumberFormatException 

Method Source Code

//package com.java2s;
/*//w  w  w .ja v a2s  . c om
 *    Geotoolkit - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2008 - 2010, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

public class Main {
    /**
     * Extends the {@link Integer#valueOf(String)} method, in removing the leading
     * and trailing whitespace.
     *
     * @param value A string representing an integer value.
     * @return An integer representation of the given string.
     * @throws NumberFormatException if the given value is {@code null}, or if the
     *                               string can't be parsed as an integer value.
     */
    public static int toInt(String value) throws NumberFormatException {
        if (value == null) {
            throw new NumberFormatException("Int value not defined.");
        }
        value = value.trim();
        return Integer.parseInt(value);
    }
}

Related

  1. toInt(String v, int def)
  2. toInt(String value)
  3. toInt(String value)
  4. toInt(String value)
  5. toInt(String value)
  6. toInt(String value, int _default)
  7. toInt(String value, int def)
  8. toInt(String value, int defaultValue)
  9. toInt(String value, int defaultValue)