Java Regex Number Validate parseNumber(String input)

Here you can find the source of parseNumber(String input)

Description

parse Number

License

Open Source License

Declaration

public static int parseNumber(String input) throws NumberFormatException 

Method Source Code


//package com.java2s;
/*//from  w ww.j ava 2 s  .c o m
 * This file is part of
 * KeepXP Server Plugin for Minecraft
 *
 * Copyright (C) 2013 Diemex
 *
 * KeepXP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * KeepXP 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 Affero Public License
 * along with KeepXP.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.regex.Pattern;

public class Main {
    private static Pattern onlyNums = Pattern.compile("[^0-9]");

    public static int parseNumber(String input) throws NumberFormatException {
        int num;
        input = onlyNums.matcher(input).replaceAll("");
        if (input.length() > 0)
            num = Integer.parseInt(input);
        else
            throw new NumberFormatException("Not a readable number \"" + input + "\"");
        return num;
    }
}

Related

  1. isNumericAndCanNull(String src)
  2. isNumericAndCanNull(String src)
  3. isNumString(String txt)
  4. parseNumber(String in)
  5. parseNumber(String input)
  6. parseNumber(String s)
  7. parseNumbers(String str)