Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please make sure that the hyphen only shows after the third digit and that when

ID: 3808430 • Letter: P

Question

Please make sure that the hyphen only shows after the third digit and that when you insert the letters I am Having FUN you get 426-4284 And when you get This Is cool you get 844-747 And that you don't get any extra hyphens like For example if you put ThE lAsT OnE You get 843-5278 Not 843--5278 Thank you.
To make telephone numbers easier to remember, some companies user 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 might use more than seven letters. For example 225-5466 can be displayed as CALL HOME, which uses eight letters. Write a Java program that prompts the user to enter a telephone number expressed in letters and outputs the comesponding telephone number in digits. If the user enters more that seven letters (spaces do not count), then process only the first seven letters and ignore the rest. Your program should also output the -hyphen) after the third digit. Allow the user to use uppercase and lowercase letters, as well as spaces between the words. Hint: You can read the entered telephone number as a string and then use the charAt method of the class string to extract each character. For example, if str refers to your String, then the expression str.charAt (i) retums the character at the i position. Recall that in a String. the position of the first character is 0. If the user enters: Get Loan Your program will output: 438-5626 Or, if the user enters: CAll HoMe Your program will output: 225-5466 Your prompt to the user should be: Please enter a telephone number using letters Please note that your class should be named TelephoneNumber.

Explanation / Answer

import java.util.Scanner;


public class Telephone {
  
public static int getIntForAlphaNumeric(char c)
{
if (c == 'S')
{
c = 'R';
}
if (c == 'Z')
{
c = 'Y';
}
int i = Character.getNumericValue(c) - Character.getNumericValue('A')+1 ;
  
return (int) (Math.ceil(i/3.0) + 1);
}
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a telephone number: ");
String line = sc.nextLine();
line = line.replaceAll("\s+","");
line = line.toUpperCase();
  
for(int i = 0; i < 3; i++)
{
System.out.print(getIntForAlphaNumeric(line.charAt(i)));
}
System.out.print("-");
for(int i = 3; i < 7; i++)
{
System.out.print(getIntForAlphaNumeric(line.charAt(i)));
}
System.out.println();
}

}