Detect Numbers in Strings

In this example you will see how to detect if a string is a number, along with its precision and scale using the NumberDetector class. This includes allowing you to determine if a number is an integer or real value and detect whether the number is signed or unsigned.

Behind the scenes, DataPipeline uses this feature in its Dataset class to analyze possible numbers and determine the best numeric Java types to hold their values.

You can see other ways to analyze data here.

package com.northconcepts.datapipeline.foundations.examples.number;

import com.northconcepts.datapipeline.foundations.number.NumberDescriptor;
import com.northconcepts.datapipeline.foundations.number.NumberDetector;
import com.northconcepts.datapipeline.foundations.number.NumberMatch;

public class DetectNumbersInStrings {

    public static void main(String[] args) {

        detectNumber("23");
        detectNumber("-236");
        detectNumber("23.49");
        detectNumber("1234567890");
        detectNumber("123456789012345678901234567890");
        detectNumber("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
        detectNumber("23L");
        detectNumber("23_3");
        detectNumber(" \t\n");
        detectNumber("");
        detectNumber(null);

    }

    public static void detectNumber(String value){
        NumberDetector detector = new NumberDetector();

        NumberMatch numberMatch = detector.match(value);

        System.out.println("=======================================================================");
        System.out.println("String: "+value);

        if (numberMatch == null) {
            System.out.println("    ------ NO MATCHING NUMBER ------");
            return;
        }

        System.out.println("    Source:          " + numberMatch.getSource());
        System.out.println("    WholePart:     " + numberMatch.getWholePart());
        System.out.println("    FractionPart:  " + numberMatch.getFractionPart());
        System.out.println("    Number:        " + numberMatch.getNumber());
        System.out.println("    Class:            " + numberMatch.getNumber().getClass());

        NumberDescriptor numberDescriptor = numberMatch.getNumberDescriptor();
        if (numberDescriptor != null) {
            System.out.println("    NumberDescriptor:");
            System.out.println("        FieldType:      " + numberDescriptor.getFieldType());
            System.out.println("        WholeDigits:    " + numberDescriptor.getWholeDigits());
            System.out.println("        Precision:      " + numberDescriptor.getPrecision());
            System.out.println("        Scale:          " + numberDescriptor.getScale());
            System.out.println("        FractionDigits: " + numberDescriptor.getFractionDigits());
            System.out.println("        IsNegative:     " + numberDescriptor.isNegative());
            System.out.println("        IsSigned:       " + numberDescriptor.isSigned());
        }
    }

}

Code walkthrough

  1. The detectNumber() method will be used to detect if the string is a number and also print additional information about the string.
  2. numberMatch.getSource() returns the input string.
  3. getWholePart() returns the digits to the left of the decimal.
  4. getFractionPart() returns the digits to the right of the decimal.
  5. getNumber() returns the input string parsed into a number.
  6. numberMatch.getNumber().getClass() returns the class of the parsed number.
  7. To get additional information on the number NumberDescriptor is used. It contains details such as FieldType, whole digits, precision, scale and more as shown in this example.

Console Output

=======================================================================
String: 23
    Source:          23
    WholePart:     23
    FractionPart:  
    Number:        23
    Class:            class java.lang.Integer
    NumberDescriptor:
        FieldType:      INT
        WholeDigits:    2
        Precision:      2
        Scale:          0
        FractionDigits: 0
        IsNegative:     false
        IsSigned:       false
=======================================================================
String: -236
    Source:          -236
    WholePart:     236
    FractionPart:  
    Number:        -236
    Class:            class java.lang.Integer
    NumberDescriptor:
        FieldType:      INT
        WholeDigits:    3
        Precision:      3
        Scale:          0
        FractionDigits: 0
        IsNegative:     true
        IsSigned:       true
=======================================================================
String: 23.49
    Source:          23.49
    WholePart:     23
    FractionPart:  49
    Number:        23.49
    Class:            class java.lang.Double
    NumberDescriptor:
        FieldType:      DOUBLE
        WholeDigits:    2
        Precision:      4
        Scale:          2
        FractionDigits: 2
        IsNegative:     false
        IsSigned:       false
=======================================================================
String: 1234567890
    Source:          1234567890
    WholePart:     1234567890
    FractionPart:  
    Number:        1234567890
    Class:            class java.lang.Long
    NumberDescriptor:
        FieldType:      LONG
        WholeDigits:    10
        Precision:      10
        Scale:          0
        FractionDigits: 0
        IsNegative:     false
        IsSigned:       false
=======================================================================
String: 123456789012345678901234567890
    Source:          123456789012345678901234567890
    WholePart:     123456789012345678901234567890
    FractionPart:  
    Number:        123456789012345678901234567890
    Class:            class java.math.BigInteger
    NumberDescriptor:
        FieldType:      BIG_INTEGER
        WholeDigits:    30
        Precision:      30
        Scale:          0
        FractionDigits: 0
        IsNegative:     false
        IsSigned:       false
=======================================================================
String: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    Source:          12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    WholePart:     12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    FractionPart:  123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    Number:        12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
    Class:            class java.math.BigDecimal
    NumberDescriptor:
        FieldType:      BIG_DECIMAL
        WholeDigits:    170
        Precision:      530
        Scale:          360
        FractionDigits: 360
        IsNegative:     false
        IsSigned:       false
=======================================================================
String: 23L
    ------ NO MATCHING NUMBER ------
=======================================================================
String: 23_3
    ------ NO MATCHING NUMBER ------
=======================================================================
String:  	

    ------ NO MATCHING NUMBER ------
=======================================================================
String: 
    ------ NO MATCHING NUMBER ------
=======================================================================
String: null
    ------ NO MATCHING NUMBER ------
Mobile Analytics