In: Computer Science
write an application that inputs a telephone number as a string in the form (555) 555_5555. the application should use string method split to extract the area code as a token. the first three digits of the phone number as a token and the last four digits of the phone number as a token. the seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed. Remember that you'll have to change delimiter characters during the tokenization process.
import java.util.Scanner; public class PhoneNumber { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.print("Enter phone number: "); String phone = sc.nextLine(); String area_code = phone.split("\\)")[0].substring(1); String last_four = phone.substring(9); System.out.println("Area code: "+area_code); System.out.println("Phone number: "+area_code+last_four); } }
Output:
======