Java Parse Int parseInt(String value)

Here you can find the source of parseInt(String value)

Description

Parse a string containing an integer with locale specific formatting.

License

Open Source License

Declaration

public static int parseInt(String value) 

Method Source Code

//package com.java2s;
/*// w w w . ja va  2s  .c o  m
 Grammatical Evolution in Java
 Release: GEVA-v2.0.zip
 Copyright (C) 2008 Michael O'Neill, Erik Hemberg, Anthony Brabazon, Conor Gilligan 
 Contributors Patrick Middleburgh, Eliott Bartley, Jonathan Hugosson, Jeff Wrigh

 Separate licences for asm, bsf, antlr, groovy, jscheme, commons-logging, jsci is included in the lib folder. 
 Separate licence for rieps is included in src/com folder.

 This licence refers to GEVA-v2.0.

 This software is distributed under the terms of the GNU General Public License.


 This program 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.

 This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 />.
 */

import java.text.DecimalFormatSymbols;

public class Main {
    /**
     * Parse a string containing an integer with locale specific formatting.
     * e.g. 1.000,00 is formatted to 1000.00 and then parsed to int
     */
    public static int parseInt(String value) {
        return Integer.parseInt(parsePrepare(value));
    }

    /**
     * Ruin all locale specific formatting for a parsable equivalent
     * @param value
     * @return
     */
    private static String parsePrepare(String value) {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        char group = symbols.getGroupingSeparator();
        char decimal = symbols.getDecimalSeparator();
        return value.trim().replace(String.valueOf(group), "")
                .replace(decimal, '.');
    }

    /**
     * Trim an array of strings so that all padding white-space is removed,
     *  e.g. {" a ", " ", "b"} -> {"a", "", "b"}
     * @param a An array of strings to trim
     * @return The array of strings with no white-space padding
     */
    public static String[] trim(String[] a) {
        for (int i = 0; i < a.length; i++)
            a[i] = a[i].trim();
        return a;
    }
}

Related

  1. parseInt(char[] input, ParsePosition offset, int length)
  2. parseInt(char[] strs, int beginindex, int endindex)
  3. parseInt(String src, Locale loc)
  4. parseInt(String stringValue)
  5. parseInteger(PushbackReader reader)
  6. parseInteger(String text)