Package org.djutils.base
Class NumberParser
java.lang.Object
org.djutils.base.NumberParser
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-2025 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 SummaryConstructorsConstructorDescriptionCreate 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 SummaryModifier and TypeMethodDescriptionintReturn the position in the original String of the first character after the parsing of the number stopped.lenient()Set the parser to lenient parsing.Set the locale for the parser to use.Set the parser to not allow for trailing characters when parsing.doubleparseDouble(String text) Parse a String and return a double value.floatparseFloat(String text) Parse a String and return a float value.intParse a String and return an int value.longParse a String and return a long value.strict()Set the parser to strict parsing.trailing()Set the parser to allow for trailing characters when parsing.
- 
Constructor Details- 
NumberParserCreate a new NumberParser, with settings for lenient parsing, whether or not to allow trailing information, and the Locale to use.- Parameters:
- trailing- whether trailing information is accepted
- lenient- when false, strict parsing according to the Locale will be performed; when true, certain violations will be accepted
- locale- the locale to use for parsing
- Throws:
- NullPointerException- when locale is null
 
- 
NumberParserpublic 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- whether trailing information is accepted
- lenient- when false, strict parsing according to the Locale will be performed; when true, certain violations will be accepted
- Throws:
- NullPointerException- when locale is null
 
- 
NumberParserpublic 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- whether trailing information is accepted
 
- 
NumberParserpublic NumberParser()Create a new NumberParser with lenient parsing, not allowing for trailing information, and using the current Locale.
 
- 
- 
Method Details- 
strictSet 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
 
- 
lenientSet 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
 
- 
trailingSet 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
 
- 
noTrailingSet 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
 
- 
localeSet 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- the new Locale to use
- Returns:
- the current NumberParser for chaining
 
- 
parseDoubleParse 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- the text to parse
- Returns:
- the double number as part of the text
- Throws:
- NumberFormatException- when the text could not be parsed given the flags
 
- 
parseFloatParse 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- the text to parse
- Returns:
- the float number as part of the text
- Throws:
- NumberFormatException- when the text could not be parsed given the flags
 
- 
parseIntParse 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- the text to parse
- Returns:
- the int number as part of the text
- Throws:
- NumberFormatException- when the text could not be parsed given the flags
 
- 
parseLongParse 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- the text to parse
- Returns:
- the long number as part of the text
- Throws:
- NumberFormatException- when the text could not be parsed given the flags
 
- 
getTrailingPositionpublic 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:
- the trailing position that denotes the first character after the parsing of the number stopped
 
 
-