Write a program in Java that does the following: 1. Asks the user for an unforma
ID: 3778922 • Letter: W
Question
Write a program in Java that does the following:
1. Asks the user for an unformatted 10 digit phone number:
It should look like this:
Enter an unformatted 10-Digit telephone number in the format XXXXXXXXXX:
Make sure that 10 digits are entered - no more or less.
2. Takes the unformatted 10-digit telephone number and formats it into a 13 character phone number with () and -
It should look like this:
Enter an unformatted 10-Digit telephone number in the format XXXXXXXXXX: 1234567890
Formatted: (123)456-7890
3. Now asks for a formatted 13-character phone number:
It should look like this:
Now enter a 13-character (including () and - ) telephone number formatted as (XXX) - XXX - XXXX
Make sure that 13 characters are entered - no more or less.
4. Takes the formatted 13-character phone number and unformats it into a 10 digit number
It should look like this:
Now enter a 13-character (including () and - ) telephone number formatted as (XXX) - XXX - XXXX
(987) - 654 - 3210
Unformatted: (987) - 654 - 3210
Explanation / Answer
import java.util.Scanner;
public class Telephone {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String a,b,c,d;
System.out.print("Enter 10 digit number:");
a = scan.next();
b = "(" +a.substring(0,3) + ")" + a.substring(3,6) +"-"+a.substring(6,10);
System.out.println("Formatted: "+b);
System.out.print("Enter 13 digit number:");
c = scan.next();
d = c.substring(1,4) + c.substring(5,8) + c.substring(9,13);
System.out.println("Unformatted: "+d);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.