Companies will occasionally list their phone number as text, rather than numbers, to make it easier for potential customers to remember. For example, '1-800-LAWYERS". Letters in the phone number String are mapped to digits like this:
ABC(2), DEF(3), GHI(4), JKL(5), MNO(6), PQRS(7), TUV(8), WXYZ(9)

Write a method that has a String parameter phone and returns a String containing the numerical equivalent for that phone number.
Convert phone to a char array and (in a loop) switch on the value of each character. I hope that you don't have 26 break statements in your program! Recall that cases without a break statement "fall" into the next case. Also, make use of the default case. Example program run (user input shown in red):
Enter phone String >>> 1-800-LAWYERS
Actual number >>> 1-800-529-9377 //use substring() to insert the last dash

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.Scanner;

public class PhoneNumbers {

public static String phoneNumber(String s) {
char[] ch = s.toCharArray();
String output = "";
int count = 0;
for(int i = 0; i < s.length(); i++) {
if(Character.isDigit(ch[i])) {
output += ch[i];
if(count == 1) {
output += "-";
if(count == 5 || count == 9) {
output += "-";
if(Character.isAlphabetic(ch[i])) {
char tmp = ch[i];
switch(tmp) {
case 'A': case 'B': case 'C':
output += "2";
$10.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats