Here you can find the source of tryParseInt(String stringInt)
Parameter | Description |
---|---|
stringInt | String to be parsed. |
public static Boolean tryParseInt(String stringInt)
//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; } }