1
1
package com .thealgorithms .conversions ;
2
2
3
3
import java .math .BigDecimal ;
4
- import java .util .*;
4
+ import java .util .ArrayDeque ;
5
+ import java .util .ArrayList ;
6
+ import java .util .HashMap ;
7
+ import java .util .List ;
8
+
5
9
6
10
/**
7
11
A Java-based utility for converting English word representations of numbers
@@ -56,12 +60,16 @@ private WordsToNumber() {
56
60
}
57
61
58
62
public static String convert (String numberInWords ) {
59
- if (numberInWords == null ) return "Null Input" ;
63
+ if (numberInWords == null ) {
64
+ return "Null Input" ;
65
+ }
60
66
61
67
String [] wordSplitArray = numberInWords .trim ().split ("[ ,-]" );
62
68
ArrayDeque <String > wordDeque = new ArrayDeque <>();
63
69
for (String word : wordSplitArray ) {
64
- if (word .isEmpty ()) continue ;
70
+ if (word .isEmpty ()) {
71
+ continue ;
72
+ }
65
73
wordDeque .add (word .toLowerCase ());
66
74
}
67
75
@@ -77,12 +85,18 @@ public static String convert(String numberInWords) {
77
85
boolean currentChunkIsZero = currentChunk .equals (BigDecimal .ZERO );
78
86
79
87
boolean isConjunction = word .equals ("and" );
80
- if (isConjunction && isValidConjunction (prevNumWasHundred , prevNumWasPowerOfTen , wordDeque )) continue ;
88
+ if (isConjunction && isValidConjunction (prevNumWasHundred , prevNumWasPowerOfTen , wordDeque )) {
89
+ continue ;
90
+ }
81
91
82
92
boolean isHundred = word .equals ("hundred" );
83
93
if (isHundred ) {
84
- if (currentChunk .compareTo (BigDecimal .TEN ) >= 0 || prevNumWasPowerOfTen ) return "Invalid Input. Unexpected Word: " + word ;
85
- if (currentChunkIsZero ) currentChunk = currentChunk .add (BigDecimal .ONE );
94
+ if (currentChunk .compareTo (BigDecimal .TEN ) >= 0 || prevNumWasPowerOfTen ) {
95
+ return "Invalid Input. Unexpected Word: " + word ;
96
+ }
97
+ if (currentChunkIsZero ) {
98
+ currentChunk = currentChunk .add (BigDecimal .ONE );
99
+ }
86
100
currentChunk = currentChunk .multiply (BigDecimal .valueOf (100 ));
87
101
prevNumWasHundred = true ;
88
102
continue ;
@@ -91,13 +105,17 @@ public static String convert(String numberInWords) {
91
105
92
106
BigDecimal powerOfTen = POWERS_OF_TEN .getOrDefault (word , null );
93
107
if (powerOfTen != null ) {
94
- if (currentChunkIsZero || prevNumWasPowerOfTen ) return "Invalid Input. Unexpected Word: " + word ;
108
+ if (currentChunkIsZero || prevNumWasPowerOfTen ) {
109
+ return "Invalid Input. Unexpected Word: " + word ;
110
+ }
95
111
BigDecimal nextChunk = currentChunk .multiply (powerOfTen );
96
112
97
- if (chunks .isEmpty () || isAdditionSafe (chunks .getLast (), nextChunk ))
113
+ if (chunks .isEmpty () || isAdditionSafe (chunks .getLast (), nextChunk )) {
98
114
chunks .add (nextChunk );
99
- else
115
+ }
116
+ else {
100
117
return "Invalid Input. Unexpected Word: " + word ;
118
+ }
101
119
currentChunk = BigDecimal .ZERO ;
102
120
prevNumWasPowerOfTen = true ;
103
121
continue ;
@@ -106,45 +124,59 @@ public static String convert(String numberInWords) {
106
124
107
125
Integer number = NUMBER_MAP .getOrDefault (word , null );
108
126
if (number != null ) {
109
- if (number == 0 && !(currentChunkIsZero && chunks .isEmpty ())) return "Invalid Input. Unexpected Word: " + word ;
127
+ if (number == 0 && !(currentChunkIsZero && chunks .isEmpty ())) {
128
+ return "Invalid Input. Unexpected Word: " + word ;
129
+ }
110
130
BigDecimal bigDecimalNumber = BigDecimal .valueOf (number );
111
131
112
- if (currentChunkIsZero || isAdditionSafe (currentChunk , bigDecimalNumber ))
132
+ if (currentChunkIsZero || isAdditionSafe (currentChunk , bigDecimalNumber )) {
113
133
currentChunk = currentChunk .add (bigDecimalNumber );
114
- else
134
+ }
135
+ else {
115
136
return "Invalid Input. Unexpected Word: " + word ;
137
+ }
116
138
continue ;
117
139
}
118
140
119
141
if (word .equals ("point" )) {
120
- if (!currentChunkIsZero ) chunks .add (currentChunk );
142
+ if (!currentChunkIsZero ) {
143
+ chunks .add (currentChunk );
144
+ }
121
145
currentChunk = BigDecimal .ZERO ;
122
146
123
147
String decimalPart = convertDecimalPart (wordDeque );
124
- if (!decimalPart .startsWith ("I" ))
148
+ if (!decimalPart .startsWith ("I" )) {
125
149
chunks .add (new BigDecimal (decimalPart ));
126
- else
150
+ }
151
+ else {
127
152
return decimalPart ;
153
+ }
128
154
break ;
129
155
}
130
156
131
157
if (word .equals ("negative" )) {
132
- if (isNegative ) return "Invalid Input. Multiple 'Negative's detected." ;
158
+ if (isNegative ) {
159
+ return "Invalid Input. Multiple 'Negative's detected." ;
160
+ }
133
161
isNegative = chunks .isEmpty () && currentChunkIsZero ;
134
162
continue ;
135
163
}
136
164
137
165
return "Invalid Input. " + (isConjunction ? "Unexpected 'and' placement" : "Unknown Word: " + word );
138
166
}
139
167
140
- if (!currentChunk .equals (BigDecimal .ZERO )) chunks .add (currentChunk );
168
+ if (!currentChunk .equals (BigDecimal .ZERO )) {
169
+ chunks .add (currentChunk );
170
+ }
141
171
BigDecimal completeNumber = combineChunks (chunks );
142
172
143
173
return isNegative ? completeNumber .multiply (BigDecimal .valueOf (-1 )).toString () : completeNumber .toString ();
144
174
}
145
175
146
176
private static boolean isValidConjunction (boolean prevNumWasHundred , boolean prevNumWasPowerOfTen , ArrayDeque <String > wordDeque ) {
147
- if (wordDeque .isEmpty ()) return false ;
177
+ if (wordDeque .isEmpty ()) {
178
+ return false ;
179
+ }
148
180
149
181
String nextWord = wordDeque .pollFirst ();
150
182
String afterNextWord = wordDeque .peekFirst ();
@@ -165,18 +197,22 @@ private static boolean isAdditionSafe(BigDecimal currentChunk, BigDecimal number
165
197
return chunkDigitCount > numberDigitCount ;
166
198
}
167
199
168
- private static String convertDecimalPart (Queue <String > wordDeque ) {
200
+ private static String convertDecimalPart (ArrayDeque <String > wordDeque ) {
169
201
StringBuilder decimalPart = new StringBuilder ("." );
170
202
while (!wordDeque .isEmpty ()) {
171
203
String word = wordDeque .poll ();
172
204
Integer number = NUMBER_MAP .getOrDefault (word , null );
173
- if (number != null )
205
+ if (number != null ) {
174
206
decimalPart .append (number );
175
- else
207
+ }
208
+ else {
176
209
return "Invalid Input. Unexpected Word (after Point): " + word ;
210
+ }
177
211
}
178
212
179
- if (decimalPart .length () == 1 ) return "Invalid Input. Decimal Part is missing Numbers." ;
213
+ if (decimalPart .length () == 1 ) {
214
+ return "Invalid Input. Decimal Part is missing Numbers." ;
215
+ }
180
216
return decimalPart .toString ();
181
217
}
182
218
@@ -188,7 +224,9 @@ private static BigDecimal combineChunks(List<BigDecimal> chunks) {
188
224
189
225
public static BigDecimal convertToBigDecimal (String numberInWords ) {
190
226
String conversionResult = convert (numberInWords );
191
- if (conversionResult .startsWith ("I" )) return null ;
227
+ if (conversionResult .startsWith ("I" )) {
228
+ return null ;
229
+ }
192
230
return new BigDecimal (conversionResult );
193
231
}
194
232
}
0 commit comments