Step 1: Add Apache Commons Lang3 to your project
The first step is to add the Apache Commons Lang3 library to your Java project.
The latest version can be found for Maven, it is 3.12.0 at the time of writing.
1
2
3
4
5
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Step 2: Convert the count to words
Once you have added the Apache Commons Lang3 library to your project, you can use it to convert a number to words. To do this, you can use the NumberToWords
class provided by the library.
Here’s an example code snippet that demonstrates how to use the NumberToWords
class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.lang3.text.NumberFormatExce;
public class NumberToWordsExample {
public static void main(String[] args) {
int number = 12345;
String numberInWords = null;
try {
numberInWords = WordUtils.capitalize(NumberToWords.convert(number));
} catch (NumberFormatException e) {
e.printStackTrace();
}
System.out.println(numberInWords);
}
}
In this example, we first import the necessary classes from the Apache Commons Lang3 library (WordUtils
and NumberToWords
). We then define a variable number
that stores the number we want to convert to words (12345
in this case).
Next, we use the NumberToWords.convert()
method to convert the number to words. This method takes an int
parameter and returns a String
representing the number in words. We then use the WordUtils.capitalize()
method to capitalize the first letter of the resulting string.
Finally, we print the resulting string to the console using the System.out.println()
method.
Step 3: Pluralize the word
To pluralize the result, we can use the PluralRules
class provided by the Apache Commons Lang3 library. This class allows us to define rules for pluralizing words based on their count.
Here’s an example code snippet that demonstrates how to use the PluralRules
class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.lang3.text.NumberFormatExce;
import org.apache.commons.lang3.text.PluralRules;
public class NumberToWordsExample {
public static void main(String[] args) {
int number = 12345;
String numberInWords = null;
try {
numberInWords = WordUtils.capitalize(NumberToWords.convert(number));
String pluralized = PluralRules.getInstance().pluralize("item", number);
numberInWords += " " + pluralized;
} catch (NumberFormatException e) {
e.printStackTrace();
}
System.out.println(numberInWords);
}
}
In this example, we first import the necessary classes from the Apache Commons Lang3 library (WordUtils
, NumberToWords
, and PluralRules
). We then define a variable number
that stores the number we want to convert to words (12345
in this case).
Next, we use the NumberToWords.convert()
method to convert the number to words, just like we did in the previous step. We then use the PluralRules.getInstance().pluralize()
method to get the plural form of the word “item” based on the number. This method takes two parameters: the singular form of the word and the count. The result is stored in the pluralized
variable.
Finally, we concatenate the pluralized form of the word to the numberInWords
string and print it to the console using the System.out.println()
method.
You now have a Java program that can convert a number to words and pluralize the result using Apache Commons Lang3.