Class NumberParser

java.lang.Object
org.djutils.base.NumberParser

public class NumberParser extends Object
NumberParser is a class that can parse a number in a strict or lenient way, and dependent on locale. It also provides help for numbers that have trailing information in the String, such as a unit. The class has been defined to use two ways of defining a parser: The first is a classical manner with a constructor that defines the settings:
   NumberParser np = new NumberParser(true, true);
   String text = "+1.127E3 m/s";
   double d = np.parseDouble(text);
   String unit = text.substring(np.getTrailingPosition()).trim();
 
or, for a simple lenient setting without trailing information:
   double d = new NumberParser().parseDouble(text);
 
Alternatively, chaining can be used:
   double d = new NumberParser().lenient().locale(Locale.US).noTrailing().parseDouble(text);
 
An instantiated NumberParser can be used multiple times, but the class is not thread-safe.

Information on how Java handles Locales from version 11 onward can be found at https://www.oracle.com/java/technologies/javase/jdk11-suported-locales.html.

Copyright (c) 2023-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.

Author:
Alexander Verbraeck
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new NumberParser with lenient parsing, not allowing for trailing information, and using the current Locale.
    NumberParser(boolean trailing)
    Create a new NumberParser with lenient parsing and using the current Locale, with a setting whether or not to allow trailing information.
    NumberParser(boolean trailing, boolean lenient)
    Create a new NumberParser, with settings for lenient parsing, whether or not to allow trailing information, and the current Locale.
    NumberParser(boolean trailing, boolean lenient, Locale locale)
    Create a new NumberParser, with settings for lenient parsing, whether or not to allow trailing information, and the Locale to use.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Return the position in the original String of the first character after the parsing of the number stopped.
    Set the parser to lenient parsing.
    locale(Locale newLocale)
    Set the locale for the parser to use.
    Set the parser to not allow for trailing characters when parsing.
    double
    Parse a String and return a double value.
    float
    Parse a String and return a float value.
    int
    Parse a String and return an int value.
    long
    Parse a String and return a long value.
    Set the parser to strict parsing.
    Set the parser to allow for trailing characters when parsing.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NumberParser

      public NumberParser(boolean trailing, boolean lenient, Locale locale)
      Create a new NumberParser, with settings for lenient parsing, whether or not to allow trailing information, and the Locale to use.
      Parameters:
      trailing - boolean; whether trailing information is accepted
      lenient - boolean; when false, strict parsing according to the Locale will be performed; when true, certain violations will be accepted
      locale - Locale; the locale to use for parsing
      Throws:
      NullPointerException - when locale is null
    • NumberParser

      public NumberParser(boolean trailing, boolean lenient)
      Create a new NumberParser, with settings for lenient parsing, whether or not to allow trailing information, and the current Locale.
      Parameters:
      trailing - boolean; whether trailing information is accepted
      lenient - boolean; when false, strict parsing according to the Locale will be performed; when true, certain violations will be accepted
      Throws:
      NullPointerException - when locale is null
    • NumberParser

      public NumberParser(boolean trailing)
      Create a new NumberParser with lenient parsing and using the current Locale, with a setting whether or not to allow trailing information.
      Parameters:
      trailing - boolean; whether trailing information is accepted
    • NumberParser

      public NumberParser()
      Create a new NumberParser with lenient parsing, not allowing for trailing information, and using the current Locale.
  • Method Details

    • strict

      public NumberParser strict()
      Set the parser to strict parsing. This method is included for chaining, so the following statement can be executed:
       new NumberParser().strict().noTrailing().locale(Locale.US).parseDouble(text);
       
      Returns:
      the current NumberParser for chaining
    • lenient

      public NumberParser lenient()
      Set the parser to lenient parsing. This method is included for chaining, so the following statement can be executed:
       new NumberParser().lenient().noTrailing().locale(Locale.US).parseDouble(text);
       
      Returns:
      the current NumberParser for chaining
    • trailing

      public NumberParser trailing()
      Set the parser to allow for trailing characters when parsing. This method is included for chaining, so the following statement can be executed:
       new NumberParser().lenient().trailing().locale(Locale.US).parseDouble(text);
       
      Returns:
      the current NumberParser for chaining
    • noTrailing

      public NumberParser noTrailing()
      Set the parser to not allow for trailing characters when parsing. This method is included for chaining, so the following statement can be executed:
       new NumberParser().lenient().noTrailing().locale(Locale.US).parseDouble(text);
       
      Returns:
      the current NumberParser for chaining
    • locale

      public NumberParser locale(Locale newLocale)
      Set the locale for the parser to use. This method is included for chaining, so the following statement can be executed:
       new NumberParser().lenient().trailing().locale(Locale.US).parseDouble(text);
       
      Parameters:
      newLocale - Locale; the new Locale to use
      Returns:
      the current NumberParser for chaining
    • parseDouble

      public double parseDouble(String text)
      Parse a String and return a double value. Independent whether lenient is true or false, leading and trailing white space will be ignored in the provided text.
      Parameters:
      text - String; the text to parse
      Returns:
      double; the double number as part of the text
      Throws:
      NumberFormatException - when the text could not be parsed given the flags
    • parseFloat

      public float parseFloat(String text)
      Parse a String and return a float value. Independent whether lenient is true or false, leading and trailing white space will be ignored in the provided text.
      Parameters:
      text - String; the text to parse
      Returns:
      float; the float number as part of the text
      Throws:
      NumberFormatException - when the text could not be parsed given the flags
    • parseInt

      public int parseInt(String text)
      Parse a String and return an int value. Independent whether lenient is true or false, leading and trailing white space will be ignored in the provided text.
      Parameters:
      text - String; the text to parse
      Returns:
      int; the int number as part of the text
      Throws:
      NumberFormatException - when the text could not be parsed given the flags
    • parseLong

      public long parseLong(String text)
      Parse a String and return a long value. Independent whether lenient is true or false, leading and trailing white space will be ignored in the provided text.
      Parameters:
      text - String; the text to parse
      Returns:
      long; the long number as part of the text
      Throws:
      NumberFormatException - when the text could not be parsed given the flags
    • getTrailingPosition

      public int getTrailingPosition()
      Return the position in the original String of the first character after the parsing of the number stopped. This means that the trailing String can be retrieved using:
       NumberParser np = new NumberParser();
       double d = np.parseDouble("12.0 m/s");
       String unit = text.substring(np.getTrailingPosition()).trim();
       
      The substring starting with the trailing position returns leading and trailing spaces.
      Returns:
      int; the trailing position that denotes the first character after the parsing of the number stopped