I need help to display my hundred and teens number in words. For example if I enter 116. My program will output One hundred and Six, instead of One hundred and sixteen. All the other numbers that I input work except for the teens numbers.
7 Answers
I would change 4 things in your code:
First:
Use int
instead of double
for your input
int numInput = Integer.parseInt(br.readLine());//user inputs number
Second:
In order to get the appropriate digit placements in int
use:
int hundredsDigit = (numInput % 1000) / 100;
int tensDigit = (numInput % 100) / 10;
int onesDigit = numInput % 10;
instead of:
double hundredsDigit=Math.floor((numInput%1000)/100);
double tensDigit = Math.floor((numInput % 100) / 10);
double onesDigit = numInput % 10;
Third:
The else
condition for the 110-119 range must be before the 100-999 (which technically should be 120-999)
Fourth:
Your teens method is taking the original numInput as parameter.
What you need to take is the onesDigit
to determine which "teen" it is
So it should be a call like:
teens(onesDigit);
This call must be changed in the [10-19] condition and the [110-119] condition
And your teens new method should look like:
public static void teens(int onesDigit) {
if (onesDigit == 0) {
System.out.print("Ten ");
}
if (onesDigit == 1) {
System.out.print("Eleven ");
}
if (onesDigit == 2) {
System.out.print("Twelve ");
}
if (onesDigit == 3) {
System.out.print("Thirteen ");
}
if (onesDigit == 4) {
System.out.print("Fourteen ");
}
if (onesDigit == 5) {
System.out.print("Fifteen ");
}
if (onesDigit == 6) {
System.out.print("Sixteen ");
}
if (onesDigit == 7) {
System.out.print("Seventeen ");
}
if (onesDigit == 8) {
System.out.print("Eighteen ");
}
if (onesDigit == 9) {
System.out.print("Nineteen ");
}
}//closes teens method

- 9,241
- 5
- 42
- 64
That happens because you check if the number is in the range [100, 999] before checking that it is in the range [100, 119], change the order of the ifs and it will work just fine.

- 174
- 2
- 2
- 9
Just in case you wan't this, I did not write this:
public static String intToText(int n) {
if (n < 0)
return "Minus " + intToText(-n);
else if (n == 0)
return "Zero";
else if (n <= 19)
return oneToNineteen[n - 1] + " ";
else if (n <= 99)
return twentyToNinety[n / 10 - 2] + " " + intToText(n % 10);
else if (n <= 199)
return "One Hundred " + intToText(n % 100);
else if (n <= 999)
return intToText(n / 100) + "Hundred " + intToText(n % 100);
else if (n <= 1999)
return "One Thousand " + intToText(n % 1000);
else if (n <= 999999)
return intToText(n / 1000) + "Thousand " + intToText(n % 1000);
else if (n <= 1999999)
return "One Million " + intToText(n % 1000000);
else if (n <= 999999999)
return intToText(n / 1000000) + "Million " + intToText(n % 1000000);
else if (n <= 1999999999)
return "One Billion " + intToText(n % 1000000000);
else
return intToText(n / 1000000000) + "Billion " + intToText(n % 1000000000);
}
Start from smallest number to highest when comparing.
First condition:
if((numInput>=10)&&(numInput<=19)){
Second:
else if((numInput>=20)&&(numInput<=99)){
Third:
else if((numInput>100)&&(numInput<=119)){
Fourth:
else if((numInput>=100)&&(numInput<=999)){

- 18,272
- 11
- 52
- 74
You should put the
else if((numInput>100)&&(numInput<=119))
clause before the more inclusive
else if((numInput>=100)&&(numInput<=999))
What's happening here is that the larger range is searched first, and your tens
function doesn't output anything for 1
.

- 506
- 2
- 6
Your else if
else if((numInput>100)&&(numInput<=119)){
hundreds(hundredsDigit);
System.out.print(" ");
teens(numInput);
}
must be placed one level higher... Since the
else if((numInput>=100)&&(numInput<=999)){
hundreds(hundredsDigit);
System.out.print(" ");
tens(tensDigit);
System.out.print(" ");
ones(onesDigit);
}
will also fulfill the condition, the "116" you entered will never get there...

- 4,875
- 3
- 39
- 60
This was fun. It has the following issues:
-Only deals with integers. No longs, doubles etc.
-Doesn't deal with the single case of zero.
-Fails at Integer.MIN_VALUE because of the number = number * -1
Otherwise, it seems to work. The motivation was that I can't stand huge blocks of "if-else" code.
public class NumbersToWords {
private static final Map<Integer,String> NUM_TO_WORD = new HashMap<Integer, String>();
private static final List<Integer> KEYS = new ArrayList<Integer>(30);
static {
NUM_TO_WORD.put(0,"zero");
NUM_TO_WORD.put(1,"one");
NUM_TO_WORD.put(2,"two");
NUM_TO_WORD.put(3,"three");
NUM_TO_WORD.put(4,"four");
NUM_TO_WORD.put(5,"five");
NUM_TO_WORD.put(6,"six");
NUM_TO_WORD.put(7,"seven");
NUM_TO_WORD.put(8,"eight");
NUM_TO_WORD.put(9,"nine");
NUM_TO_WORD.put(10,"ten");
NUM_TO_WORD.put(11,"eleven");
NUM_TO_WORD.put(12,"twelve");
NUM_TO_WORD.put(13,"thirteen");
NUM_TO_WORD.put(14,"fourteen");
NUM_TO_WORD.put(15,"fifteen");
NUM_TO_WORD.put(16,"sixteen");
NUM_TO_WORD.put(17,"seventeen");
NUM_TO_WORD.put(18,"eighteen");
NUM_TO_WORD.put(19,"nineteen");
NUM_TO_WORD.put(20,"twenty");
NUM_TO_WORD.put(30,"thirty");
NUM_TO_WORD.put(40,"forty");
NUM_TO_WORD.put(50,"fifty");
NUM_TO_WORD.put(60,"sixty");
NUM_TO_WORD.put(70,"seventy");
NUM_TO_WORD.put(80,"eighty");
NUM_TO_WORD.put(90,"ninety");
NUM_TO_WORD.put(100,"hundred");
NUM_TO_WORD.put(1000,"thousand");
NUM_TO_WORD.put(1000000,"million");
NUM_TO_WORD.put(1000000000,"billion");
KEYS.addAll(NUM_TO_WORD.keySet());
Collections.sort(KEYS);
}
public static void main(String[] args){
int[] testValues = {24,4,543755,12,10000,123000,123,Integer.MAX_VALUE, -456};
NumbersToWords ntw = new NumbersToWords();
for(int i : testValues){
System.out.println(i + " -> " + ntw.getWords(i));
}
}
/* called recursively */
public String getWords(int number){
boolean isNegative = number < 0;
if(isNegative){
number = number * -1;
}
if(number < 100){
return getWordLessThanHundred(number);
}
StringBuilder words = new StringBuilder(50);
int key = getKey(number);
if(isNegative){
words.append("negative ");
}
words.append(getWords(number/key))
.append(" ").append(NUM_TO_WORD.get(key)) // get the largest placeholder word
.append(" ").append(getWords(number % key)); // get the rest
return words.toString();
}
private String getWordLessThanHundred(int number){
if(number == 0){
return "";
}
if(number < 21){
return NUM_TO_WORD.get(number);
}
int key = getKey(number);
return NUM_TO_WORD.get(key) + " " + NUM_TO_WORD.get(number - key);
}
private int getKey(int number){
for(int i = 0; i<KEYS.size();i++){
int value = KEYS.get(i);
if(i > 0 && number < value){
return KEYS.get(i - 1);
}else if(number == value){
return value;
}
}
return KEYS.get(KEYS.size() - 1);
}
}

- 3,749
- 1
- 16
- 27