Java Regex Number Validate parseNumber(String s)

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

Description

parse Number

License

Open Source License

Declaration

public static long parseNumber(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.HashMap;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//from   ww  w .  j  a  va  2  s.  c  o m
     * Static members used for identifying ordinal numbers.
     */
    private static final String[] onesNumbers = { "zero", "one", "two",
            "three", "four", "five", "six", "seven", "eight", "nine",
            "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
            "sixteen", "seventeen", "eighteen", "nineteen" };
    private static final String[] tensNumbers = { "twenty", "thirty",
            "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    private static final String[] scalesNumbers = { "hundred", "thousand",
            "million", "billion" };
    private static final int[] scalesInts = { 100, 1000, 1000000,
            1000000000 };
    private static final String[] ordinalWords = { "first", "second",
            "third", "fifth", "eighth", "ninth", "twelfth" };
    private static final int[] ordinalWordValues = { 1, 2, 3, 5, 8, 9, 12 };
    private static final Pattern numericOrdSuffix = Pattern
            .compile("(\\d+)(st|nd|rd|th)$");
    private static HashMap<String, Integer> scales;
    private static HashMap<String, Integer> increments;
    private static HashMap<String, Integer> ordinals;
    private static HashMap<String, Integer> numberValues;
    private static boolean isInitialized = false;

    public static long parseNumber(String s) {
        if (!isInitialized)
            init();
        s = s.toLowerCase();
        try {
            return Long.parseLong(s);
        } catch (NumberFormatException e) {
            if (numberValues.containsKey(s)) {
                return numberValues.get(s);
            } else {
                return parseOrdinal(s);
            }
        }
    }

    private static void init() {
        increments = new HashMap<String, Integer>();
        scales = new HashMap<String, Integer>();
        numberValues = new HashMap<String, Integer>();
        for (int i = 0; i < onesNumbers.length; i++) {
            increments.put(onesNumbers[i], i);
            numberValues.put(onesNumbers[i], i);
            scales.put(onesNumbers[i], 1);
        }
        for (int i = 0; i < tensNumbers.length; i++) {
            increments.put(tensNumbers[i], 10 * (i + 2));
            numberValues.put(tensNumbers[i], 10 * (i + 2));
            scales.put(tensNumbers[i], 1);
        }
        for (int i = 0; i < scalesNumbers.length; i++) {
            increments.put(scalesNumbers[i], 0);
            scales.put(scalesNumbers[i], scalesInts[i]);
            numberValues.put(scalesNumbers[i], scalesInts[i]);
        }

        ordinals = new HashMap<String, Integer>();
        for (int i = 0; i < ordinalWords.length; i++) {
            ordinals.put(ordinalWords[i], ordinalWordValues[i]);
        }

        isInitialized = true;
    }

    public static int parseOrdinal(String s) {
        if (!isInitialized)
            init();

        s = s.replace('-', ' ').toLowerCase();

        // Is numeric with a suffix like -th
        Matcher m = numericOrdSuffix.matcher(s);
        if (m.matches()) {
            String intStr = m.group(1);
            return Integer.parseInt(intStr);
        }

        String[] words = s.split("\\s+");
        String lastWord = words[words.length - 1];
        if (!isBasicOrdinal(lastWord))
            return -1;

        int result = 0;
        int current = 0;
        for (String word : words) {
            int scale, increment;
            if (ordinals.containsKey(word)) {
                scale = 1;
                increment = ordinals.get(word);
            } else {
                if (word.endsWith("ieth")) {
                    word = word.substring(0, word.length() - 4) + "y";
                } else if (word.endsWith("th")) {
                    word = word.substring(0, word.length() - 2);
                }
                if (scales.containsKey(word)
                        && increments.containsKey(word)) {
                    scale = scales.get(word);
                    increment = increments.get(word);
                } else {
                    return -1;
                }
            }
            current = current * scale + increment;
            if (scale > 100) {
                result += current;
                current = 0;
            }
        }

        return result + current;

    }

    public static boolean isBasicOrdinal(String word) {
        if (ordinals.containsKey(word)) {
            return true;
        } else {
            if (word.endsWith("ieth")) {
                word = word.substring(0, word.length() - 4) + "y";
            } else if (word.endsWith("th")) {
                word = word.substring(0, word.length() - 2);
            } else {
                return false;
            }
            return scales.containsKey(word) && increments.containsKey(word);
        }
    }
}

Related

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