Java problem: A common programming activity involves the extraction and processi
ID: 660630 • Letter: J
Question
Java problem:
A common programming activity involves the extraction and processing of data contained in a string, such as separating a sentence into individual words. We typically refer to these separate words as tokens.
The purpose of this exercise is to develop a program that tokenizes an input string.
Can somone write a program that inputs a telephone number as a string in the form (555) 555-5555. The program 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 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.
Once you have a working version of the program, extend it to process an input string that contains multiple, comma separated, phone numbers, such as
(555) 555-5555, (444) 444-4444, (333) 333-3333). For the purpose of this exercise assume that the input string contains well-formed telephone numbers.
Note: it is Java program
Thanks in advance
Explanation / Answer
/**
* The java program that prompts to enter comma,separated
* string phone numbers and divides the phone numbers into
* area code and telephone numbers using split and StringTokenizer
* class
* */
//TelephoneProgram.java
import java.util.Scanner;
import java.util.StringTokenizer;
public class TelephoneProgram
{
public static void main(String[] args)
{
//Scanner class object to read input from keyboard
Scanner input=new Scanner(System.in);
//To read input phone numbers
String inputLine;
System.out.println("Enter comma separated ,multiple telephone numbers");
System.out.println("(xxx)xxx-xxxx,(xxx)xxx-xxxx,");
//prompt for user input
inputLine=input.nextLine();
//Using split method
/* Call the method split that splits the inputLine string into
an array of string phone numbers*/
String[] phoneNumbers=inputLine.split(",");
//Iterate over the string of phone numbers
for (String phoneNumber : phoneNumbers)
{
//Using StringTokenizer class
/*create an instance of StringTokenizer class that
takes the phoneNumber string and seprates the phone numbers using
- hyphen */
StringTokenizer tokens=
new StringTokenizer(phoneNumber, "-");
/*Extract the first token using nextToken method and
call the method substring that gets the string from index 1 to substring
of index 4 */
System.out.println("Tele-phone Number : "+phoneNumber);
System.out.println("Area Code : "+tokens.nextToken().substring(1, 4));
//print next phone number string
System.out.println("Phone Number : "+tokens.nextToken());
}//end of foreach loop
}//end of main method
}//end of the class TelephoneProgram
----------------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Enter comma separated ,multiple telephone numbers
(xxx)xxx-xxxx,(xxx)xxx-xxxx,
(555)555-5555,(444)444-4444,(333)333-3333
Tele-phone Number : (555)555-5555
Area Code : 555
Phone Number : 5555
Tele-phone Number : (444)444-4444
Area Code : 444
Phone Number : 4444
Tele-phone Number : (333)333-3333
Area Code : 333
Phone Number : 3333
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.