Java Parse Int parseInteger(String text)

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

Description

Returns text converted to an integer value or throws an exception.

License

Open Source License

Parameter

Parameter Description
text string to convert to a number
def default value to use if a ParseException is thrown

Return

the result

Declaration

public static Integer parseInteger(String text) throws ParseException 

Method Source Code


//package com.java2s;
/*//from   ww w  .  java 2 s. c  om
 * This software copyright by various authors including the RPTools.net
 * development team, and licensed under the LGPL Version 3 or, at your option,
 * any later version.
 * 
 * Portions of this software were originally covered under the Apache Software
 * License, Version 1.1 or Version 2.0.
 * 
 * See the file LICENSE elsewhere in this distribution for license details.
 */

import java.text.NumberFormat;
import java.text.ParseException;

public class Main {
    private static NumberFormat nf = NumberFormat.getNumberInstance();

    /**
     * Returns <code>text</code> converted to an integer value, or the value of
     * <code>def</code> if the string cannot be converted. This method is
     * locale-aware (which doesn't mean much for integers).
     * 
     * @param text
     *            string to convert to a number
     * @param def
     *            default value to use if a ParseException is thrown
     * @return the result
     */
    public static Integer parseInteger(String text, Integer def) {
        if (text == null)
            return def;
        try {
            return parseInteger(text);
        } catch (ParseException e) {
            return def;
        }
    }

    /**
     * Returns <code>text</code> converted to an integer value or throws an
     * exception. This method is locale-aware (which doesn't mean much for
     * integers).
     * 
     * @param text
     *            string to convert to a number
     * @param def
     *            default value to use if a ParseException is thrown
     * @return the result
     */
    public static Integer parseInteger(String text) throws ParseException {
        int def = 0;
        if (text == null)
            return def;
        def = nf.parse(text).intValue();
        //      System.out.println("Integer:  Input string is >>"+text+"<< and parsing produces "+newValue);
        return def;
    }
}

Related

  1. parseInt(char[] strs, int beginindex, int endindex)
  2. parseInt(String src, Locale loc)
  3. parseInt(String stringValue)
  4. parseInt(String value)
  5. parseInteger(PushbackReader reader)