Question: JAVA: I am tokenizing a telephone number of the format (555)555-5555. My code so far is posted below. I must use the String split method

JAVA: I am tokenizing a telephone number of the format (555)555-5555. My code so far is posted below. I must use the String split method only. How do I add text to my ouput ( System.out.println(token))

I want to add the words area code before 111, first three numbers before 111, and last four numbers before 1111 of the phone number.

Is there a way to concatenate System.out.println(token) to add the wording I want before each token is printed to the screen?

Here is my code so far:

import java.util.Scanner; //import scanner

class tokenizeNumber { //class

public static void main(String[] args) { //main method

Scanner scanner = new Scanner(System.in); //declare & intialize new scanner to read user input

System.out.println("Please enter phone number and press enter: "); //prompt user to enter phone number

String phoneNumber = scanner.nextLine(); //read and store user input in variable named phoneNumber

//validate phone number. Should be a number 1-9 AND be in format (555)555-5555

//public static boolean validatePhone(String phoneNumber) {

// return phoneNumber.matches( "\\([1-9]\\)\\d{3}-[1-9]\\d{3}-\\d{4}" );

//process phone number

String[] tokens = phoneNumber.split("-|\\(|\\)");

for (String token : tokens) {

System.out.println(token);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!