Java Regex Int Validate parseInteger(String attrVal)

Here you can find the source of parseInteger(String attrVal)

Description

Returns the integer represented by attrVal or Integer.MIN_VALUE on error.

License

Open Source License

Parameter

Parameter Description
attrVal a string representing an integer attribute value (can be <code>null</code>)

Return

the integer represented by attrVal or Integer.MIN_VALUE on error

Declaration

public static int parseInteger(String attrVal) 

Method Source Code

//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;
        }
    }
}

Related

  1. parseInt(Object str)
  2. parseInt(String value)
  3. parseInt(String[] strs)
  4. parseInteger(String inStr, Integer def)
  5. parseInteger(String key, String sourceString)
  6. parseInteger(String str)
  7. parseIntegerList(String var0, int var1, int var2)