Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PhoneNumbers
- {
- /**
- Cleans a phone number.
- @param phoneNumber a phone number that should contain ten digits and possibly other characters
- @return the phone number in the form (###) ###-#### or the string "Error" if phoneNumber
- does not have ten digits
- */
- public String cleanNumber(String phoneNumber)
- {
- // your work here
- String number = "";
- for (int i = 0; i < phoneNumber.length(); i++)
- {
- char ch = phoneNumber.charAt(i);
- if (Character.isDigit(ch))
- {
- number += ch;
- }
- }
- if(number.length() != 10){return "Error";}
- return String.format("(%c%c%c) %c%c%c-%c%c%c%c"
- , number.charAt(0), number.charAt(1), number.charAt(2)
- , number.charAt(3), number.charAt(4), number.charAt(5)
- , number.charAt(6), number.charAt(7), number.charAt(8)
- , number.charAt(9));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement