-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem17.java
65 lines (64 loc) · 2.7 KB
/
Problem17.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
/**
* Created by Jackson on 5/22/16.
*/
public class Problem17 {
static String[] singleDigitNums = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
static String[] teenDigitNums = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
static String[] doubleDigitNums = {"","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public static void main(String[] args){
String number;
String word;
int letterCount = 0;
for(int i=1;i<=1000;i++){
word = getWord(i + "");
System.out.println(word);
letterCount += word.length();
}
System.out.println("The letter count is " + letterCount);
}
public static String getWord(String number){
String word;
int numberCount = number.length();
if(numberCount == 1) word = singleDigitWord(number);
else if(numberCount == 2) word = doubleDigitWord(number);
else if(numberCount == 3) word = tripleDigitWord(number);
else word = "Onethousand";
return word;
}
public static String singleDigitWord(String number){
String word = "";
for(int i=1;i<=9;i++){
if(Integer.parseInt(number) == i){
word = singleDigitNums[i - 1];
break;
}
}
return word;
}
public static String doubleDigitWord(String number){
int tensPlaceInt = Integer.parseInt(number.charAt(0) + "");
int onesPlaceInt = Integer.parseInt(number.charAt(1) + "");
String tensPlaceString = "";
String onesPlaceString = "";
if(tensPlaceInt == 1) return teenDigitNums[Integer.parseInt(number) - 10];
for(int i=1;i<=9;i++){
if(onesPlaceInt == i) onesPlaceString = singleDigitNums[i - 1];
if(tensPlaceInt == i) tensPlaceString = doubleDigitNums[i - 1];
}
return tensPlaceString + onesPlaceString;
}
public static String tripleDigitWord(String number){
int oneHundredsPlaceInt = Integer.parseInt(number.charAt(0) + "");
String oneHundredsPlaceString = "";
String onesAndTensPlace = doubleDigitWord(number.substring(1));
String word;
for(int i=1;i<=9;i++){
if(oneHundredsPlaceInt == i) oneHundredsPlaceString = singleDigitNums[i - 1] + "Hundred";
}
word = oneHundredsPlaceString + onesAndTensPlace;
if(!onesAndTensPlace.equals("")) word = oneHundredsPlaceString + "and" + onesAndTensPlace;
else word = oneHundredsPlaceString + onesAndTensPlace;
return word;
}
}