SHARE
TWEET

Untitled

advictoriam Jan 15th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class PhoneNumbers
  2. {
  3.    /**
  4.       Cleans a phone number.
  5.       @param phoneNumber a phone number that should contain ten digits and possibly other characters
  6.       @return the phone number in the form (###) ###-#### or the string "Error" if phoneNumber
  7.       does not have ten digits
  8.    */
  9.    public String cleanNumber(String phoneNumber)
  10.    {  
  11.       // your work here
  12.       String number = "";
  13.       for (int i = 0; i < phoneNumber.length(); i++)
  14.       {
  15.          char ch = phoneNumber.charAt(i);
  16.          if (Character.isDigit(ch))
  17.          {
  18.             number += ch;
  19.          }          
  20.       }
  21.       if(number.length() != 10){return "Error";}
  22.       return String.format("(%c%c%c) %c%c%c-%c%c%c%c"
  23.       , number.charAt(0), number.charAt(1), number.charAt(2)
  24.       , number.charAt(3), number.charAt(4), number.charAt(5)
  25.       , number.charAt(6), number.charAt(7), number.charAt(8)
  26.       , number.charAt(9));
  27.    }  
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top