Here you can find the source of parseInteger(String attrVal)
attrVal
or Integer.MIN_VALUE
on error.
Parameter | Description |
---|---|
attrVal | a string representing an integer attribute value (can be <code>null</code>) |
attrVal
or Integer.MIN_VALUE
on error
public static int parseInteger(String attrVal)
//package com.java2s; //License from project: Open Source License import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**// ww w. j a v a2 s . c om * The pattern for extracting an integer by skipping white space and ignoring * trailing garbage. */ private static Pattern INTEGER_PATTERN = Pattern.compile("^[ \t\n\r]*(-?[0-9]+)"); /** * Returns the integer represented by <code>attrVal</code> or * <code>Integer.MIN_VALUE</code> on error. * * @param attrVal a string representing an integer attribute * value (can be <code>null</code>) * @return the integer represented by <code>attrVal</code> or * <code>Integer.MIN_VALUE</code> on error */ public static int parseInteger(String attrVal) { if (attrVal == null) { return Integer.MIN_VALUE; } Matcher m = INTEGER_PATTERN.matcher(attrVal); if (!m.matches()) { return Integer.MIN_VALUE; } try { return Integer.parseInt(m.group(1)); } catch (NumberFormatException e) { return Integer.MIN_VALUE; } } }