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

Write a Java program that inputs a ten-digit phone number, and then turns that t

ID: 3783932 • Letter: W

Question

Write a Java program that inputs a ten-digit phone number, and then turns that ten- digit phone number into a more readable string with a pair of parentheses, a dash, and a space.

For example, if the user enters the value 5147447500 as input, your program should print the string (514) 744-7500.

Hint use integer division and integer remainder operations to compute the three required blocks of numbers: area code 514, prefix 744, and line number 7500.

For example, in Java, 5147447500L/ 10000000 is equal to 514.

A sample run of your program should produce the following output, where the user’s input is shown inredfor clarity:

Enter a 10-digit phone-number: 5147447500

area code:514

prefix : 744

line number: 7500

PhoneNumber:(514) 744-7500

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;

class PhoneNumber
{
   public static void main (String[] args)
   {
       Scanner scan = new Scanner(System.in);
      
       System.out.println("Enter a 10-digit phone-number:");
      
       String phone = scan.next();
      
       //use substring() method to extract characters from phone string
       String areaCode = phone.substring(0,3);
       String prefix = phone.substring(3,6);
       String lineNumber = phone.substring(6,10);
      
       //display different parts of phone number
       System.out.println("area code : "+areaCode);
       System.out.println("prefix : "+prefix);
       System.out.println("lineNumber : " +lineNumber);
      
       System.out.println("Phone Number :("+areaCode+") "+prefix+"-"+lineNumber); //display in format
   }
}

output:

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