Hello , All . need to write the below java program To make telephone numbers eas
ID: 3851488 • Letter: H
Question
Hello , All . need to write the below java program
To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies must use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters.
Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more than seven letters, then process only the first seven letters. Also output the – (hyphen) after the third digit. Allow the user to use both uppercase and lowercase letters as well as spaces between words.
code optimization: no unnecessary variables , if statements and counters
sample output screen shown with 3 cases(atleast one with blank spaces ect.)Descriptive block and inline comments include.
Thanks in advance
Explanation / Answer
FILE NAME:CONVERSION.JAVA
import java.util.Scanner;
public class Conversion {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int sum = 0;
System.out.println("enter the input :");
String input=sc.nextLine();
String sb=input.toLowerCase();
for(int i=0;i<sb.length();i++)
{
if (sb.charAt(i) != ' ' && sb.charAt(i) >= 'a' && sb.charAt(i) <= 'z' && sb.charAt(i) >= 'A' && sb.charAt(i) <= 'z') sum++;
if(sum == 4) {System.out.print("-");}
switch (sb.charAt(i)) {
case 'a':
case 'b':
case 'c':
System.out.print( "2");
break;
case 'd':
case 'e':
case 'f':
System.out.print( "3");
break;
case 'g':
case 'h':
case 'i':
System.out.print( "4");
break;
case 'j':
case 'k':
case 'l':
System.out.print( "5");
break;
case 'm':
case 'n':
case 'o':
System.out.print( "6");
break;
case 'p':
case 'q':
case 'r':
case 's':
System.out.print( "7");
break;
case 't':
case 'u':
case 'v':
System.out.print( "8");
break;
case 'w':
case 'x':
case 'y':
case 'z':
System.out.print( "9");
System.out.print( " ");
break;
default:
System.out.print( "9");
}
}
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.