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

Modify to accept a telephone number with any number of letters. The output shoul

ID: 3759034 • Letter: M

Question

Modify to accept a telephone number with any number of letters. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.

import java.util.Scanner;

public class AssignmentQ3 {
public static void main(String[] args)
{String number;
int i=0,j=0,len;
char c;
Scanner in=new Scanner (System.in);
do
{i=0;
j=0;
System.out.println("Enter the phone number: ");
number=in.nextLine();
number=number.toUpperCase();
len=number.length();
while(i<len&&j<8)
{c=number.charAt(i);
switch(c)
{case 'A':case 'B':case 'C':
System.out.print("2");
j++;
break;
case 'D':case 'E':case 'F':
System.out.print("3");
j++;
break;
case 'G':case 'H':case 'I':
System.out.print("4");
j++;
break;
case 'J':case 'K':case 'L':
System.out.print("5");
j++;
break;
case 'M':case 'N':case 'O':
System.out.print("6");
j++;
break;
case 'P':case 'R':case 'S':
System.out.print("7");
j++;
break;
case 'T':case 'U':case 'V':
System.out.print("8");
j++;
break;
case 'W':case 'X':case 'Y':
System.out.print("9");
j++;
break;
}
if(j==3)
{System.out.print("-");
j++;
}
i++;
}

System.out.print(" Do it again(Y/N)? ");
number=in.nextLine();
number=number.toUpperCase();
}while(number.charAt(0)=='Y');
}
}

Explanation / Answer

package chegg;

import java.util.Scanner;

public class AssignmentQ3 {

   public static void main(String[] args) {

       String number;

       int i = 0, j = 0, len;

       char c;

       Scanner in = new Scanner(System.in);

       do {

           i = 0;

           j = 0;

           System.out.println("Enter the phone number: ");

           number = in.nextLine();

           number = number.toUpperCase();

           len = number.length();

           while (i < len && j < 8) {

               c = number.charAt(i);

              

               switch (c) {

               case 'A':

               case 'B':

               case 'C':

                   System.out.print("2");

                   j++;

                   break;

               case 'D':

               case 'E':

               case 'F':

                   System.out.print("3");

                   j++;

                   break;

               case 'G':

               case 'H':

               case 'I':

                   System.out.print("4");

                   j++;

                   break;

               case 'J':

               case 'K':

               case 'L':

                   System.out.print("5");

                   j++;

                   break;

               case 'M':

               case 'N':

               case 'O':

                   System.out.print("6");

                   j++;

                   break;

               case 'P':

               case 'R':

               case 'S':

                   System.out.print("7");

                   j++;

                   break;

               case 'T':

               case 'U':

               case 'V':

                   System.out.print("8");

                   j++;

                   break;

               case 'W':

               case 'X':

               case 'Y':

                   System.out.print("9");

                   j++;

                   break;

               default:

                   System.out.print(c);

                   j++;

                   break;

               }

               if (j == 3 || ( (j -3)%4 == 0)) {

                   System.out.print("-");

               }

               i++;

           }

           System.out.print(" Do it again(Y/N)? ");

           number = in.nextLine();

           number = number.toUpperCase();

       } while (number.charAt(0) == 'Y');

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote