Skip to content

Commit 00c709e

Browse files
committed
solve build error, resolve PMD Warning
1 parent 0b59139 commit 00c709e

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/main/java/com/thealgorithms/conversions/WordsToNumber.java

+35-20
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public static String convert(String numberInWords) {
7979
boolean prevNumWasHundred = false;
8080
boolean prevNumWasPowerOfTen = false;
8181

82-
while (!wordDeque.isEmpty()) {
82+
String errorMessage = null;
83+
84+
while (!wordDeque.isEmpty() && errorMessage == null) {
8385
String word = wordDeque.poll();
8486
boolean currentChunkIsZero = currentChunk.compareTo(BigDecimal.ZERO) == 0;
8587

@@ -88,10 +90,10 @@ public static String convert(String numberInWords) {
8890
continue;
8991
}
9092

91-
boolean isHundred = word.equals("hundred");
92-
if (isHundred) {
93+
if (word.equals("hundred")) {
9394
if (currentChunk.compareTo(BigDecimal.TEN) >= 0 || prevNumWasPowerOfTen) {
94-
return "Invalid Input. Unexpected Word: " + word;
95+
errorMessage = "Invalid Input. Unexpected Word: " + word;
96+
continue;
9597
}
9698
if (currentChunkIsZero) {
9799
currentChunk = currentChunk.add(BigDecimal.ONE);
@@ -105,15 +107,16 @@ public static String convert(String numberInWords) {
105107
BigDecimal powerOfTen = POWERS_OF_TEN.getOrDefault(word, null);
106108
if (powerOfTen != null) {
107109
if (currentChunkIsZero || prevNumWasPowerOfTen) {
108-
return "Invalid Input. Unexpected Word: " + word;
110+
errorMessage = "Invalid Input. Unexpected Word: " + word;
111+
continue;
109112
}
110113
BigDecimal nextChunk = currentChunk.multiply(powerOfTen);
111114

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;
116118
}
119+
chunks.add(nextChunk);
117120
currentChunk = BigDecimal.ZERO;
118121
prevNumWasPowerOfTen = true;
119122
continue;
@@ -123,14 +126,15 @@ public static String convert(String numberInWords) {
123126
Integer number = NUMBER_MAP.getOrDefault(word, null);
124127
if (number != null) {
125128
if (number == 0 && !(currentChunkIsZero && chunks.isEmpty())) {
126-
return "Invalid Input. Unexpected Word: " + word;
129+
errorMessage = "Invalid Input. Unexpected Word: " + word;
130+
continue;
127131
}
128132
BigDecimal bigDecimalNumber = BigDecimal.valueOf(number);
129133

130134
if (currentChunkIsZero || isAdditionSafe(currentChunk, bigDecimalNumber)) {
131135
currentChunk = currentChunk.add(bigDecimalNumber);
132136
} else {
133-
return "Invalid Input. Unexpected Word: " + word;
137+
errorMessage = "Invalid Input. Unexpected Word: " + word;
134138
}
135139
continue;
136140
}
@@ -145,20 +149,25 @@ public static String convert(String numberInWords) {
145149
if (!decimalPart.startsWith("I")) {
146150
chunks.add(new BigDecimal(decimalPart));
147151
} else {
148-
return decimalPart;
152+
errorMessage = decimalPart;
149153
}
150-
break;
154+
continue;
151155
}
152156

153157
if (word.equals("negative")) {
154158
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;
156162
}
157-
isNegative = chunks.isEmpty() && currentChunkIsZero;
158163
continue;
159164
}
160165

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;
162171
}
163172

164173
if (!(currentChunk.compareTo(BigDecimal.ZERO) == 0)) {
@@ -195,14 +204,20 @@ private static boolean isAdditionSafe(BigDecimal currentChunk, BigDecimal number
195204

196205
private static String convertDecimalPart(ArrayDeque<String> wordDeque) {
197206
StringBuilder decimalPart = new StringBuilder(".");
207+
String errorMessage = null;
208+
198209
while (!wordDeque.isEmpty()) {
199210
String word = wordDeque.poll();
200211
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;
205215
}
216+
decimalPart.append(number);
217+
}
218+
219+
if (errorMessage != null) {
220+
return errorMessage;
206221
}
207222

208223
if (decimalPart.length() == 1) {

0 commit comments

Comments
 (0)