For the input -2147483648
, your program outputs just minus
. Also, you never handle 0
anywhere.
In convert()
, I personally wouldn't bother with int units = num;
, and I'd just use num
.
Instead of instantiating a StringBuilder
in convertHundredsImpl()
, I'd just pass the original StringBuilder
to the helper function. For consistency then, I'd also pass a StringBuilder
to the convertUnitsImpl()
helper. In accordance with the design of the StringBuilder
class, I'd have those helpers return the same StringBuilder
as well, to allow for fluent chaining, like convertHundredsImpl(sb, thousands).append(" thousand ")
.
The while (true)
loop in main()
gives you no graceful way to exit. It should be while (scanner.hasNextInt())
instead.