Skip to content

Commit 565640f

Browse files
authored
Update TextDollar.java
1 parent 7df72ed commit 565640f

File tree

1 file changed

+18
-55
lines changed

1 file changed

+18
-55
lines changed

Hard-Level/TextDollar.java

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,41 @@
22
import java.util.*;
33
public class Main {
44
public static void main (String[] args) throws IOException {
5-
File file = new File(args[0]);
6-
BufferedReader br = new BufferedReader(new FileReader(file));
5+
BufferedReader br = new BufferedReader(new FileReader(new File(args[0])));
76
String line;
8-
Main m = new Main();
97
while ((line = br.readLine()) != null) {
108
int i = Integer.parseInt(line);
11-
if (i==0) {
12-
System.out.println("ZeroDollars");
13-
}
14-
else {
15-
System.out.println(m.word(i) +"Dollars");
16-
}
9+
String a = (i == 0) ? "ZeroDollars": new Main().word(i) +"Dollars";
10+
System.out.println(a);
1711
}
1812
br.close();
1913
}
14+
2015
String word(int i) {
2116
Map<Integer, String> m = new HashMap<Integer, String>();
22-
m.put(1, "One");
23-
m.put(2, "Two");
24-
m.put(3, "Three");
25-
m.put(4, "Four");
26-
m.put(5, "Five");
27-
m.put(6, "Six");
28-
m.put(7, "Seven");
29-
m.put(8, "Eight");
30-
m.put(9, "Nine");
31-
m.put(10, "Ten");
32-
m.put(11, "Eleven");
33-
m.put(12, "Twelve");
34-
m.put(13, "Thirteen");
35-
m.put(14, "Fourteen");
36-
m.put(15, "Fifteen");
37-
m.put(16, "Sixteen");
38-
m.put(17, "Seventeen");
39-
m.put(18, "Eighteen");
40-
m.put(19, "Nineteen");
41-
m.put(20, "Twenty");
42-
m.put(30, "Thirty");
43-
m.put(40, "Forty");
44-
m.put(50, "Fifty");
45-
m.put(60, "Sixty");
46-
m.put(70, "Seventy");
47-
m.put(80, "Eighty");
48-
m.put(90, "Ninety");
17+
m.put(1, "One"); m.put(2, "Two"); m.put(3, "Three");m.put(4, "Four");
18+
m.put(5, "Five"); m.put(6, "Six"); m.put(7, "Seven"); m.put(8, "Eight");
19+
m.put(9, "Nine"); m.put(10, "Ten"); m.put(11, "Eleven"); m.put(12, "Twelve");
20+
m.put(13, "Thirteen"); m.put(14, "Fourteen"); m.put(15, "Fifteen");
21+
m.put(16, "Sixteen"); m.put(17, "Seventeen"); m.put(18, "Eighteen");
22+
m.put(19, "Nineteen"); m.put(20, "Twenty"); m.put(30, "Thirty");
23+
m.put(40, "Forty"); m.put(50, "Fifty"); m.put(60, "Sixty");
24+
m.put(70, "Seventy"); m.put(80, "Eighty"); m.put(90, "Ninety");
4925
if (i > 0 && i <= 20) {
5026
return m.get(i);
5127
}
5228
if (i > 20 && i < 100) {
53-
int y = i-(i%10);
54-
int z = i%10;
55-
return m.get(y)+this.word(z);
29+
return m.get(i-(i%10))+this.word(i%10);
5630
}
5731
if (i >= 100 && i < 1000) {
58-
int y = i-(i%100);
59-
int z = i%100;
60-
return m.get(y/100)+"Hundred"+this.word(z);
32+
return m.get((i-(i%100))/100)+"Hundred"+this.word(i%100);
6133
}
62-
if (i >= 1000 && i < 10000) {
63-
int y = i-(i%1000);
64-
int z = i%1000;
65-
return m.get(y/1000)+"Thousand"+this.word(z);
66-
}
67-
if (i >= 10000 && i < 1000000) {
68-
int y = i-(i%1000);
69-
int z = i%1000;
70-
return this.word(y/1000)+"Thousand"+this.word(z);
34+
if (i >= 1000 && i < 1000000) {
35+
return this.word((i-(i%1000))/1000)+"Thousand"+this.word(i%1000);
7136
}
7237
if (i >= 1000000 && i < 1000000000) {
73-
int y = i-(i%1000000);
74-
int z = i%1000000;
75-
return this.word(y/1000000)+"Million"+this.word(z);
38+
return this.word((i-(i%1000000))/1000000)+"Million"+this.word(i%1000000);
7639
}
77-
return "";
40+
return "";
7841
}
7942
}

0 commit comments

Comments
 (0)