Here you can find the source of parseInteger(String text)
text
converted to an integer value or throws an exception.
Parameter | Description |
---|---|
text | string to convert to a number |
def | default value to use if a ParseException is thrown |
public static Integer parseInteger(String text) throws ParseException
//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; } }