-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordConverter.java
123 lines (107 loc) · 4.7 KB
/
WordConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package word_converter_fn;
import java.util.ArrayList;
/**
* Number to word converter.
* @author elmiguel
*/
public class WordConverter {
// Arrays representing ones, elevens, tens, and large number groupings
private static final String[] ONES = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
private static final String[] ELEVENS = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
private static final String[] TENS = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
private static final String[] GROUPING = {"hundred", "thousand", "million", "billion", "trillion", "quadrillion", "sextillion", "septillion", "octillion", "nonillion", "decillion"};
/**
* Returns the corresponding word for a given character digit in the given array.
*/
public static String getDigit(String[] type, char digit) {
if (type == TENS) digit -= 2; // Adjust for tens index
return type[digit - '0'].trim();
}
/**
* Processes a two-digit number and converts it to words.
*/
public static String processDouble(char first, char second) {
String result = (first == '1') ? getDigit(ELEVENS, second)
: getDigit(TENS, first) + " " + getDigit(ONES, second);
// Remove trailing "zero" if necessary
if (result.endsWith("zero")) {
result = result.substring(0, result.length() - 4);
}
return result.trim();
}
/**
* Processes a three-digit number and converts it to words.
*/
public static String processTriple(char hundreds, char tens, char ones, int set) {
String hundredsPart = (hundreds == '0') ? "" : getDigit(ONES, hundreds) + " " + GROUPING[0];
String tensOnesPart = (tens == '0') ? getDigit(ONES, ones) : processDouble(tens, ones);
String suffix = GROUPING[set];
return (hundredsPart + " " + tensOnesPart + " " + suffix).trim();
}
/**
* Pads a number string with leading zeros to ensure it has a length that is a multiple of three.
*/
public static String prePad(String number, int pad) {
switch (pad) {
case 1: return "00" + number; // Pad two zeros for one digit
case 2: return "0" + number; // Pad one zero for two digits
default: return number;
}
}
/**
* Processes a list of three-digit number groups and converts them to words.
*/
public static String process(ArrayList<String> numbers) {
StringBuilder result = new StringBuilder();
String[] numArray = numbers.toArray(new String[0]);
for (int i = 0; i < numArray.length; i++) {
int setIndex = (numArray.length - 1) - i;
result.append(" ").append(processTriple(numArray[i].charAt(0), numArray[i].charAt(1), numArray[i].charAt(2), setIndex));
}
// Remove the trailing grouping suffix and trim the result
return result.substring(0, result.length() - 8).trim();
}
/**
* Converts a numeric string into its word equivalent.
*/
public static String convert(String number) {
String result = "";
int length = number.length();
// Handle single-digit numbers
if (length == 1) {
return getDigit(ONES, number.charAt(0));
}
// Handle two-digit numbers
if (length == 2) {
if (number.charAt(0) == '1') {
return getDigit(ELEVENS, number.charAt(1)); // Special case for numbers between 10 and 19
} else {
result = processDouble(number.charAt(0), number.charAt(1));
}
}
// Handle numbers with three or more digits
if (length >= 3) {
// Pad the number with zeros to make its length a multiple of three
number = prePad(number, number.length() % 3);
ArrayList<String> groups = new ArrayList<>();
for (int i = 0; i <= (number.length() - 1) / 3; i++) {
groups.add(number.substring(i * 3, (i * 3) + 3));
}
result = process(groups);
}
return result.trim();
}
public static void main(String[] args) {
for (String arg : args) {
if (arg != null && !arg.isEmpty()) {
String input = arg.trim();
boolean isNegative = input.startsWith("-");
if (isNegative) {
input = input.substring(1);
}
String output = (isNegative ? "negative " : "") + convert(input);
System.out.println("Input: " + arg + "\nOutput: " + output);
}
}
}
}