Hello, I need help to to add modify the program to process as many telephone num
ID: 3931860 • Letter: H
Question
Hello, I need help to to add modify the program to process as many telephone numbers as the user wants and also to improve/simply my final output to fulfill this requirement - The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after four digits. My program below: import java.util.*; public class teleAlphaTwo { public static void main(String [] args) { Scanner gaz = new Scanner (System.in); int length, num; int count=0; String letters, output=""; System.out.println("Please type the words to be converted to a phone number:"); letters = gaz.nextLine().toLowerCase(); length = letters.length(); for (num=0; num<=length-1; num++) { switch (letters.charAt(num)) { case 'a': case 'b': case 'c': output+="2"; count++; break; case 'd': case 'e': case 'f': output+="3"; count++; break; case 'g': case 'h': case 'i': output+="4"; count++; break; case 'j': case 'k': case 'l': output+="5"; count++; break; case 'm': case 'n': case 'o': output+="6"; count++; break; case 'p': case 'q': case 'r': case 's': output+="7"; count++; break; case 't': case 'u': case 'v': output+="8"; count++; break; case 'w': case 'x': case 'y': case 'z': output+="9"; count++; break; default: output=output; break; } System.out.println("The phone number is "+output.substring(0, 3)+"-"+output.substring(3, 7)+"-"+output.substring(7, 11)+"-"+output.substring(11, 15)); } } }
Explanation / Answer
Java Code:
import java.util.Scanner;
public class TeleAlphaTwo {
public static void main(String [] args) {
Scanner gaz = new Scanner(System.in);
int length=0;
int count=0;
String letters, output="";
System.out.println("Please type the words to be converted to a phone number:");
letters = gaz.nextLine().toLowerCase();
for(int num=0;num<letters.length();num++){
switch (letters.charAt(num)) {
case 'a': case 'b': case 'c': output+="2"; count++; break;
case 'd': case 'e': case 'f': output+="3"; count++; break;
case 'g': case 'h': case 'i': output+="4"; count++; break;
case 'j': case 'k': case 'l': output+="5"; count++; break;
case 'm': case 'n': case 'o': output+="6"; count++; break;
case 'p': case 'q': case 'r': case 's': output+="7";count++; break;
case 't': case 'u': case 'v': output+="8"; count++; break;
case 'w': case 'x': case 'y': case 'z': output+="9"; count++; break;
default: output=output; break;
}
}
int itval=1;String newoutput= output.substring(0,3)+"-";
for(int i=3;i<output.length();++i){
if((3+(4*itval))==i){
newoutput += "-";
itval++;
}
newoutput += output.charAt(i);
}
System.out.print("The phone number is "+newoutput);
}
}
Output:
Please type the words to be converted to a phone number:
bharathkuma
The phone number is 242-7284-5862
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.