public String cleanNumber(String phoneNumber) { int digit=0; String result = ""; String number= ""; for (int i = 0; i < phoneNumber.length(); i++) { char ch = phoneNumber.charAt(i); if (Character.isDigit(ch)) { number = number + ch; digit++; } } if(digit == 10) System.out.println("("+phoneNumber.substring(1,3)+")"+"-"+phoneNumber.substring(4,6)+phoneNumber.substring(7,10)); else result = "Error"; return result; } } Argument "(444) 999-1234" Actual (44)-) 99- "" Expected "(444) 999-1234" "4449991234" (44)-99234 "" "(444) 999-1234" "444.999.1234" (44)-99.12 "" "(444) 999-1234" What am doing wrong? How can I make the actual look like the expected? What do I have to change?
public String cleanNumber(String phoneNumber)
{
int digit=0;
String result = "";
String number= "";
for (int i = 0; i < phoneNumber.length(); i++)
{
char ch = phoneNumber.charAt(i);
if (Character.isDigit(ch))
{
number = number + ch;
digit++;
}
}
if(digit == 10)
System.out.println("("+phoneNumber.substring(1,3)+")"+"-"+phoneNumber.substring(4,6)+phoneNumber.substring(7,10));
else
result = "Error";
return result;
}
}
Argument "(444) 999-1234" |
Actual (44)-) 99- "" |
Expected "(444) 999-1234" |
"4449991234" | (44)-99234 "" | "(444) 999-1234" |
"444.999.1234" | (44)-99.12 "" | "(444) 999-1234" |
What am doing wrong? How can I make the actual look like the expected? What do I have to change?
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
- Recommended textbooks for youDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSONDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSONC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag…Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education