@@ -79,7 +79,9 @@ public static String convert(String numberInWords) {
79
79
boolean prevNumWasHundred = false ;
80
80
boolean prevNumWasPowerOfTen = false ;
81
81
82
- while (!wordDeque .isEmpty ()) {
82
+ String errorMessage = null ;
83
+
84
+ while (!wordDeque .isEmpty () && errorMessage == null ) {
83
85
String word = wordDeque .poll ();
84
86
boolean currentChunkIsZero = currentChunk .compareTo (BigDecimal .ZERO ) == 0 ;
85
87
@@ -88,10 +90,10 @@ public static String convert(String numberInWords) {
88
90
continue ;
89
91
}
90
92
91
- boolean isHundred = word .equals ("hundred" );
92
- if (isHundred ) {
93
+ if (word .equals ("hundred" )) {
93
94
if (currentChunk .compareTo (BigDecimal .TEN ) >= 0 || prevNumWasPowerOfTen ) {
94
- return "Invalid Input. Unexpected Word: " + word ;
95
+ errorMessage = "Invalid Input. Unexpected Word: " + word ;
96
+ continue ;
95
97
}
96
98
if (currentChunkIsZero ) {
97
99
currentChunk = currentChunk .add (BigDecimal .ONE );
@@ -105,15 +107,16 @@ public static String convert(String numberInWords) {
105
107
BigDecimal powerOfTen = POWERS_OF_TEN .getOrDefault (word , null );
106
108
if (powerOfTen != null ) {
107
109
if (currentChunkIsZero || prevNumWasPowerOfTen ) {
108
- return "Invalid Input. Unexpected Word: " + word ;
110
+ errorMessage = "Invalid Input. Unexpected Word: " + word ;
111
+ continue ;
109
112
}
110
113
BigDecimal nextChunk = currentChunk .multiply (powerOfTen );
111
114
112
- if (chunks .isEmpty () || isAdditionSafe (chunks .getLast (), nextChunk )) {
113
- chunks .add (nextChunk );
114
- } else {
115
- return "Invalid Input. Unexpected Word: " + word ;
115
+ if (!(chunks .isEmpty () || isAdditionSafe (chunks .getLast (), nextChunk ))) {
116
+ errorMessage = "Invalid Input. Unexpected Word: " + word ;
117
+ continue ;
116
118
}
119
+ chunks .add (nextChunk );
117
120
currentChunk = BigDecimal .ZERO ;
118
121
prevNumWasPowerOfTen = true ;
119
122
continue ;
@@ -123,14 +126,15 @@ public static String convert(String numberInWords) {
123
126
Integer number = NUMBER_MAP .getOrDefault (word , null );
124
127
if (number != null ) {
125
128
if (number == 0 && !(currentChunkIsZero && chunks .isEmpty ())) {
126
- return "Invalid Input. Unexpected Word: " + word ;
129
+ errorMessage = "Invalid Input. Unexpected Word: " + word ;
130
+ continue ;
127
131
}
128
132
BigDecimal bigDecimalNumber = BigDecimal .valueOf (number );
129
133
130
134
if (currentChunkIsZero || isAdditionSafe (currentChunk , bigDecimalNumber )) {
131
135
currentChunk = currentChunk .add (bigDecimalNumber );
132
136
} else {
133
- return "Invalid Input. Unexpected Word: " + word ;
137
+ errorMessage = "Invalid Input. Unexpected Word: " + word ;
134
138
}
135
139
continue ;
136
140
}
@@ -145,20 +149,25 @@ public static String convert(String numberInWords) {
145
149
if (!decimalPart .startsWith ("I" )) {
146
150
chunks .add (new BigDecimal (decimalPart ));
147
151
} else {
148
- return decimalPart ;
152
+ errorMessage = decimalPart ;
149
153
}
150
- break ;
154
+ continue ;
151
155
}
152
156
153
157
if (word .equals ("negative" )) {
154
158
if (isNegative ) {
155
- return "Invalid Input. Multiple 'Negative's detected." ;
159
+ errorMessage = "Invalid Input. Multiple 'Negative's detected." ;
160
+ } else {
161
+ isNegative = chunks .isEmpty () && currentChunkIsZero ;
156
162
}
157
- isNegative = chunks .isEmpty () && currentChunkIsZero ;
158
163
continue ;
159
164
}
160
165
161
- return "Invalid Input. " + (isConjunction ? "Unexpected 'and' placement" : "Unknown Word: " + word );
166
+ errorMessage = "Invalid Input. " + (isConjunction ? "Unexpected 'and' placement" : "Unknown Word: " + word );
167
+ }
168
+
169
+ if (errorMessage != null ) {
170
+ return errorMessage ;
162
171
}
163
172
164
173
if (!(currentChunk .compareTo (BigDecimal .ZERO ) == 0 )) {
@@ -195,14 +204,20 @@ private static boolean isAdditionSafe(BigDecimal currentChunk, BigDecimal number
195
204
196
205
private static String convertDecimalPart (ArrayDeque <String > wordDeque ) {
197
206
StringBuilder decimalPart = new StringBuilder ("." );
207
+ String errorMessage = null ;
208
+
198
209
while (!wordDeque .isEmpty ()) {
199
210
String word = wordDeque .poll ();
200
211
Integer number = NUMBER_MAP .getOrDefault (word , null );
201
- if (number != null ) {
202
- decimalPart .append (number );
203
- } else {
204
- return "Invalid Input. Unexpected Word (after Point): " + word ;
212
+ if (number == null ) {
213
+ errorMessage = "Invalid Input. Unexpected Word (after Point): " + word ;
214
+ break ;
205
215
}
216
+ decimalPart .append (number );
217
+ }
218
+
219
+ if (errorMessage != null ) {
220
+ return errorMessage ;
206
221
}
207
222
208
223
if (decimalPart .length () == 1 ) {
0 commit comments