I am doing a beginner Java course and working on methods. My assignment is to print inputted numbers between 1-999 as words. All of them work except for any "teen" number in the hundreds (110, 114, 212, 919, etc.) The regular teens work fine, but my method for any 3digit numbers to identify -teens (as they would have a second digit of 1) doesn't.
-
It is always recommended that you prepare an [SSCCE](http://sscce.org) in order to get quick and better answers. – Lion Jun 09 '13 at 13:47
-
See this question, and see that it was closed: http://stackoverflow.com/questions/1720049/print-number-in-words – Raedwald Jun 09 '13 at 13:49
-
Do you mean I should post the entire program? – user2466525 Jun 09 '13 at 13:49
-
1@user2466525 : No you should post only the erroneous part of your code, precisely isolated and concise. – Lion Jun 09 '13 at 13:53
-
execute me , can you give me example for your output and the expected one ? ,because i can't understand the question – Alya'a Gamal Jun 09 '13 at 13:54
-
`hundredsTeens` is an unnecessary variable as it is the same as `onesDigit`. Also, make the types of all of your variables `int` instead of `double`, since they are integers. Also, `(numInput % 100) % 10` can be simplified to `numInput % 10`. – Bernhard Barker Jun 09 '13 at 13:57
-
Ok -- I removed the unnecessary parts in my coding. What I don't understand, however, is why the teens method should be skipped over. My output for "114" would be "ONE HUNDRED FOUR" instead of "ONE HUNDRED FORTEEN". – user2466525 Jun 09 '13 at 13:59
-
but ONE HUNDRED FORTEEN" is right !!! ONE HUNDRED FOUR is equal to 104 – Alya'a Gamal Jun 09 '13 at 14:03
-
[Seems to be working just fine](http://ideone.com/XBlE5t). – Bernhard Barker Jun 09 '13 at 14:04
-
When I run it, it says "ONE HUNDRED FOUR" instead of "ONE HUNDRED FOURTEEN" – user2466525 Jun 09 '13 at 14:06
3 Answers
I ran your program and it seemed to be working fine for the -teens
in the 3 digits (e.g. 110,114,etc.)
However, it was not working for the two digit -teens
like 14, 17, etc.
Your code
if((numInput>=10)&&(numInput<=19)){//will initiate the teens subroutine
teens(numInput);}
Should be changed to
if((numInput>=10)&&(numInput<=19)){//will initiate the teens subroutine
teens(onesDigit);}
The teens()
subroutine takes the ones digit as a parameter, and not the whole number.
Also, there is no need for the hundredsTeens
variable. You could just pass the onesDigit
instead.

- 5,410
- 1
- 37
- 55
-
Wow, it runs fine for those? It does not run fine for me....and my 2 digit -teen numbers work fine. Strange. – user2466525 Jun 09 '13 at 14:07
-
2I saw the revised edition too, but that is wrong. I am getting the erroneous results in the revised edition. The original version was relatively error-free. Firstly, you do not need two different methods - `hundredTeens` and `teens`. Only one of them would do the job. Do not use double; instead use int. Double will be inappropriate with the modulo %. In your revised edition the teens are two digit numbers. Instead of that, make them one digit numbers and pass only the onesDigit to that method. Hope that helps. – aksh1t Jun 09 '13 at 14:39
-
The last one is working perfectly for me. `ONE HUNDRED FIFTEEN` for 115 and so on... You could try and restart your IDE or something. Or maybe take a new notepad and recompile it. – aksh1t Jun 09 '13 at 14:52
I think you forgot something in your code.
You have identifying two cases : the ten digit is 1 or not. There are three cases.
1- <1
2- ==1
3- >1
Then you use double tensDigit = (numInput % 100) / 10; but this it not a digit! 114 returns 1.4, you should declare a digit as an integer.
Try this first (with digit as double...) :
if (tensDigit < 1){
ones(onesDigit); // only display the last digit
}
else if(1==Integer.parseInt(Double.toString(tensDigit).substring(0, 1))){
teens(hundredsTeens);
}
else if (tensDigit > 1){
tens(tensDigit);
System.out.print(" ");
ones(onesDigit);
}
}
You will see your mistake, and then try to put real digit to symplify your code readability.

- 232
- 1
- 2
- 9
-
Thanks, I created additional if statements the second digit being less than or greater than 1. – user2466525 Jun 09 '13 at 14:56
Here is the answer to print numbers from 0 to 99999 in words using some Java 8. Run the program passing the number as command line argument.
public static final Map<Integer, String> DICTIONARY = Collections.unmodifiableMap(Stream
.of(new SimpleEntry<>(0, "zero"), new SimpleEntry<>(1, "one"), new SimpleEntry<>(2, "two"),
new SimpleEntry<>(3, "three"), new SimpleEntry<>(4, "four"), new SimpleEntry<>(5, "five"),
new SimpleEntry<>(6, "six"), new SimpleEntry<>(7, "seven"), new SimpleEntry<>(8, "eight"),
new SimpleEntry<>(9, "nine"), new SimpleEntry<>(10, "ten"), new SimpleEntry<>(11, "eleven"),
new SimpleEntry<>(12, "tweleve"), new SimpleEntry<>(13, "thirteen"), new SimpleEntry<>(14, "fourteen"),
new SimpleEntry<>(15, "fifteen"), new SimpleEntry<>(16, "sixteen"), new SimpleEntry<>(17, "seventeen"),
new SimpleEntry<>(18, "eighteen"), new SimpleEntry<>(19, "nineteen"), new SimpleEntry<>(20, "twenty"),
new SimpleEntry<>(30, "thirty"), new SimpleEntry<>(40, "forty"), new SimpleEntry<>(50, "fifty"),
new SimpleEntry<>(60, "sixty"), new SimpleEntry<>(70, "seventy"), new SimpleEntry<>(80, "eighty"),
new SimpleEntry<>(90, "ninety"), new SimpleEntry<>(100, "hundred"), new SimpleEntry<>(1000, "thousand"))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
public static void main(String args[]) {
try {
if (args.length == 0 || args.length > 1) {
throw new RuntimeException(
"Please run the program with only one number in the range of 0 to 99999");
}
Integer givenNumber = Integer.parseInt(args[0]);
NumbersEnglishDictionary.DICTIONARY.entrySet()
.stream()
.filter(e -> e.getKey().equals(givenNumber))
.findAny()
.ifPresent(System.out::print);
if (givenNumber < 100) {
System.out.print(givenNumber + " = " + NumbersEnglishDictionary.DICTIONARY.get((givenNumber / 10) * 10)
+ " " + NumbersEnglishDictionary.DICTIONARY.get(givenNumber % 10));
return;
}
if (givenNumber > 100 && givenNumber < 1000) {
System.out.print(givenNumber + " = " + NumbersEnglishDictionary.DICTIONARY.get(givenNumber / 100)
+ " hundred " + NumbersEnglishDictionary.DICTIONARY.get(((givenNumber / 10) % 10) * 10) + " "
+ NumbersEnglishDictionary.DICTIONARY.get(givenNumber % 10));
return;
}
if (givenNumber > 1000 && givenNumber < 10000) {
System.out.print(givenNumber + " = " + NumbersEnglishDictionary.DICTIONARY.get(givenNumber / 1000)
+ " thousand " + NumbersEnglishDictionary.DICTIONARY.get((givenNumber / 100) % 10) + " hundred "
+ NumbersEnglishDictionary.DICTIONARY.get(((givenNumber / 10) % 10) * 10) + " "
+ NumbersEnglishDictionary.DICTIONARY.get(givenNumber % 10));
return;
}
if (givenNumber > 10000 && givenNumber < 100000) {
System.out.print(
givenNumber + " = " + NumbersEnglishDictionary.DICTIONARY.get(((givenNumber / 1000) / 10) * 10)
+ " " + NumbersEnglishDictionary.DICTIONARY.get((givenNumber / 1000) % 10)
+ " thousand " + NumbersEnglishDictionary.DICTIONARY.get((givenNumber / 100) % 10)
+ " hundred " + NumbersEnglishDictionary.DICTIONARY.get(((givenNumber / 10) % 10) * 10)
+ " " + NumbersEnglishDictionary.DICTIONARY.get(givenNumber % 10));
return;
}
} catch (NumberFormatException e) {
throw new RuntimeException("Please run the program with only one number in the range of 0 to 99999");
}
}

- 980
- 1
- 8
- 27