-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoneNum-wordToNumb.java
52 lines (38 loc) · 1.23 KB
/
phoneNum-wordToNumb.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
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String ans = getPhoneNumber(s);
for (int i = 0; i < ans.length(); i++) {
System.out.print(ans.charAt(i));
}
}
static String[] num = {"zero","one","two","three","four","five","six","seven","eight","nine"};
public static String getPhoneNumber(String s) {
// Write your code here
String ans = "";
int j=0;
String[] word = s.split(" ");
/*for (int i = 0; i < word.length; i++) {
System.out.print(word[i]+" ");
}*/
while(j<word.length){
String temp = word[j];
if(temp.equals("double")){
j++;
temp = word[j];
for(int i=1;i<2;i++){
ans += Arrays.asList(num).indexOf(temp);
}
}else if(temp.equals("triple")){
j++;
temp = word[j];
for(int i=1;i<3;i++){
ans += Arrays.asList(num).indexOf(temp);
}
}else {
ans += Arrays.asList(num).indexOf(temp);
j++;
}
}
return ans;
}