Question: I need the DASH it needs to display 356-9377 here is my code please explain the fix. import java.util.*; public class Telephone { public static
I need the DASH it needs to display "356-9377"
here is my code please explain the fix.
import java.util.*;
public class Telephone {
public static void main(String[] args) {
String sevenChar = "flowers"; //356-9377
System.out.println(Convert(sevenChar));
}
public static String Convert(String word){
//HashMap
//char[] charWord = word.toCharArray();
String temp = "";
for(int i = 0; i < word.length(); i++){
switch(word.charAt(i)){
case 'a':
case 'b':
case 'c':
temp += '2';
break;
case 'd':
case 'e':
case 'f':
temp += '3';
break;
case 'g':
case 'h':
case 'i':
temp += '4';
break;
case 'j':
case 'k':
case 'l':
temp += '5';
break;
case 'm':
case 'n':
case 'o':
temp += '6';
break;
case 'p':
case 'q':
case 'r':
case 's':
temp += '7';
break;
case 't':
case 'u':
case 'v':
temp += '8';
break;
case 'w':
case 'x':
case 'y':
case 'z':
temp += '9';
break;
}
}
String dash = "-";
String newString = "";
for(int i = 0; i < temp.length(); i++){
newString += temp.charAt(i);
if(temp.charAt(i) == 3){
newString += dash;
}
}
return newString;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
