/
WordtoDigit.java
64 lines (59 loc) · 1.69 KB
/
WordtoDigit.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
/**
* Word to Digit
* --------------
* @author adambose1990
*
* CHALLENGE DESCRIPTION:
* Having a string representation of a set of numbers you need to print this numbers.
* All numbers are separated by semicolon. There are up to 20 numbers in one line.
* The numbers are "zero" to "nine"
*
* INPUT SAMPLE:
* Your program should accept as its first argument a path to a filename. Each line in this file
* is one test case. E.g.
* zero;two;five;seven;eight;four
* three;seven;eight;nine;two
*
* OUTPUT SAMPLE:
* Print numbers in the following way:
* 025784
* 37892
*/
package com.codeeval.easy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class WordtoDigit {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
File file = new File("files/easy/test_WordtoDigit.txt");
BufferedReader buffer = new BufferedReader(new FileReader(file));
String line;
while ((line = buffer.readLine()) != null) {
line = line.trim();
String[] codes = line.split(";");
StringBuffer buff = new StringBuffer();
for (String s : codes) {
buff.append(getWordFromLibrary(s));
}
System.out.println(buff);
}
}
private static String getWordFromLibrary(String key) {
Map<String, String> library = new HashMap<String, String>();
library.put("one", "1");
library.put("two", "2");
library.put("three", "3");
library.put("four", "4");
library.put("five", "5");
library.put("six", "6");
library.put("seven", "7");
library.put("eight", "8");
library.put("nine", "9");
library.put("zero", "0");
return library.get(key);
}
}