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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started