** I cant seem to figure out how to incorporate splitting a
string from user input. Any help would be appreciated.
Next write a
Java program that enters a 10-digit string as a typical U.S. telephone number, extracts the 3-digit area code, the 3-digit �exchange,� and the remaining 4-digit number as separate strings, prints them and then prints the complete telephone number in the usual formatting. A sample might look like this:
Enter 10-digit telephone number: 1234567890
You entered 1234567890
The area code is 123
The
exchange is 456
The number is 7890
The complete telephone number is (123) 456-7890
******************************************************************
import java.io.*;
public class ReadString {
public static void main (String[] args) {
// prompt the user to enter their phone
System.out.print("Enter 10-digit telephone number: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String phone = null;
// read the input from the command-line; need to use try/catch with the
// readLine() method
try {
phone = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your phone number!");
System.exit(1);
}
System.out.println("The number you entered is: " + phone);
System.out.println("The area code is: " + phone);
System.out.println("The exchange is: " + phone);
System.out.println("The number is: " + phone);
}
} // end of ReadString class
*****************************************************************
public class PhoneString
{
public static void main(String[] args){
String s = "9721234567";
System.out.println(s);
String areaCode = s.substring(0,3);
String exchange = s.substring(3,6);
String number = s.substring(6,10);
System.out.println(areaCode);
System.out.println(exchange);
System.out.println(number);
System.out.println("You have entered the following phone number");
System.out.println( "(" + areaCode + ") " + exchange + "-" + number);
}
} // end of PhoneString class