need help on raptor part Many websites ask for phone numbers. The problem is tha
ID: 3576922 • Letter: N
Question
need help on raptor part
Many websites ask for phone numbers. The problem is that there are so many different ways to represent a phone number. Examples include 817-555-1234, 817 555 1234 (c), and (817) 555-1234 x23. Write a Raptor program which inputs a string containing a phone number in any format and outputs it in the standard format. For this assignment, the standard format is (817) 555-1234.
Sample Output(inputs in bold) Please enter a phone number: 817-555-1234 The properly formatted number is (817) 555-1234 Please enter a phone number: (817)515 7259 x23 The phone number must have 10 digits Please enter a phone number: 214 555 9999 (c) The properly formatted number is (214) 555-9999 Please enter a phone number: 800**4444xxx333 The properly formatted number is (800) 444-4333
Explanation / Answer
package chegg1.com;
import java.util.Scanner;
public class PhoneNumberFormat {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String choice = "";
do{
System.out.println("Please Enter the phone number : ");
String input = sc.nextLine();
String correctFormat = formatPhoneNumber(input);
if(correctFormat.equalsIgnoreCase("Invalid")){
System.out.println(" The phone number must have 10 digits ");
}else{
System.out.println("Propely formatted number is : "+ correctFormat);
}
System.out.println("do you want test more : (y/?) : ");
choice = sc.nextLine();
}while(choice.equalsIgnoreCase("y"));
}
private static String formatPhoneNumber(String input) {
byte[] bytes = input.getBytes();
int lengthCount = 0;
String properNumber="";
for (byte tempByt : bytes) {
char tempChar = (char) tempByt;
// checking if the number is digit then merge the number with properformattednumber
if (Character.isDigit(tempChar)) {
if(lengthCount == 0){
properNumber = "(";
}
if(lengthCount == 3){
properNumber = properNumber + ")";
}
if(lengthCount == 6){
properNumber = properNumber + "-";
}
properNumber = properNumber + tempChar;
lengthCount++;
}
}
if(lengthCount == 10){
return properNumber;
}
else{
return "Invalid";
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.