In: Computer Science
Tokenizing Telephone Numbers Write java 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;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
// user input
Scanner sc = new Scanner(System.in);
String ph = sc.nextLine();
String[] phone = new String[2];
String[] fl = new String[2];
String code, token1, token2, num;
// split using space that retrieves code
phone = ph.split(" ");
code = phone[0].substring(1,4);
// split using - retrives 3 and last 4
fl = phone[1].split("-");
token1 = fl[0];
token2 = fl[1];
// putting 2 tokens together
num = token1+token2;
// printing code and number
System.out.println("Code is "+code);
System.out.println("Number is "+num);
}
}
/*
Sample output:
(123) 456-7890
Code is 123
Number is 4567890
*/