Java Integer Parse tryParseInt(String stringInt)

Here you can find the source of tryParseInt(String stringInt)

Description

Checks if a string is possible to parse into an integer or not.

License

Open Source License

Parameter

Parameter Description
stringInt String to be parsed.

Return

Boolean

Declaration

public static Boolean tryParseInt(String stringInt) 

Method Source Code

//package com.java2s;
/**// w  w  w  .  j  a v  a  2s  .  c  om
 * Small utility class created to do tasks that occur often.
 * 
 * @author Emil Carlsson
 * 
 * This file is a part of Doris
 *
 * Doris 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.
 * Doris 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 Doris.  
 * If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    /**
     * Checks if a string is possible to parse into an integer or not.
     * @param stringInt String to be parsed.
     * @return Boolean
     */
    public static Boolean tryParseInt(String stringInt) {
        try {
            Integer.parseInt(stringInt);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * Parses a String into an integer. Returns 0 if not parsable.
     * @param stringInt String to be parsed.
     * @return Integer.
     */
    public static int parseInt(String stringInt) {
        if (tryParseInt(stringInt)) {
            return Integer.parseInt(stringInt);
        }

        return 0;
    }
}

Related

  1. tryParseInt(String intString, int defaultValue)
  2. tryParseInt(String num)
  3. tryParseInt(String s)
  4. tryParseInt(String str)
  5. tryParseInt(String str, int defaultValue)
  6. tryParseInt(String text)
  7. tryParseInt(String value)
  8. tryParseInt(String value)
  9. tryParseInteger(String intValue)