Java Integer Create toIntOrNull(String string)

Here you can find the source of toIntOrNull(String string)

Description

tries to parse the value as int or in case of an error returns null

License

Open Source License

Parameter

Parameter Description
string a parameter

Declaration

public static Integer toIntOrNull(String string) 

Method Source Code

//package com.java2s;
/*/*from w  w  w . j a va 2 s.  com*/
 * Copyright (C) 2016 Florian Frankenberger.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * tries to parse the value as int or in case of an error returns null
     *
     * @param string
     * @return
     */
    public static Integer toIntOrNull(String string) {
        Integer i = null;
        try {
            i = Integer.valueOf(string);
        } catch (NumberFormatException e) {
            //ignore
        }
        return i;
    }
}

Related

  1. toIntFormat(String value)
  2. toIntHeader(String value)
  3. toIntLE(byte[] value)
  4. toIntList(String valus)
  5. toIntMatrix(Number[][] matrix)
  6. toIntPlusOneString(Object object)
  7. toIntRound(double[] a)
  8. toInts(byte[] bytes)
  9. toInts(byte[] readBuffer, int o, int l)