Using Java write a Program FixPhone.java that fixes the phone number entry error
ID: 3626932 • Letter: U
Question
Using Java write a Program FixPhone.java that fixes the phone number entry error,ask for the phone number .check if the phone number is fixable,if not ask for another phone number or display invalid result?How to fix:
Check the length of the characters
remove parentheses.
remove dashes.
If it is fixable fix it.
use isNumber method to validate.
program never stops until user terminates by typing “quit”(make it Case insensitive) (hint: equalsIgnorecase ,use infinite loop mechanism)
Fix it by stripping all the dashes and parentheses then validate the input .
Right format of phone#:(XXX)XXX-XXXX
This is how i had started;
/*Progammer: John Marwa.
Objective: To correct entry errors in telephone numbers.
*/
import java.util.*;
public class FixTELNUM
{
public static void main(String[] args)
{
//String variable to hold user input.
String telnum;
//Create a Scanner object for keyboard input.
Scanner sc=new Scanner(System.in);
System.out.print("Type telephone Number: ");
telnum=sc.nextLine();
//Replace opening parenthes in a telephone number.
telnum=telnum.replaceAll("(","");
//Replace Closing parenthes in a phone#.
telnum=telnum.replaceAll(")","");
//Replace dash in phone#.
telnum=telnum.replaceAll("-","");
//Declare the variables.
String first,middle,last;
//Select the first part of the telephone# to correct.
first=telnum.substring(1,3);
//Select the middle part of the telephone # to correct.
middle=telnum.substring(5,3);
//Select the last part of the telephone # to correct.
last=telnum.substring(9,5);
//Declare the variables.
telnum="("+first+")"+middle+"-"+last;
//Display correct telephone number#
System.out.println("telnum= "+telnum);
}
}
Explanation / Answer
please rate - thanks
try this
import java.util.*;
public class FixPhone
{
public static void main(String args[])
{String phone;
boolean valid;
Scanner in = new Scanner(System.in);
System.out.print("Enter a phone number: ");
phone=in.nextLine();
while(!phone.equalsIgnoreCase("quit"))
{System.out.print(phone);
if(isValid(phone))
System.out.println(": Valid phone number");
else
{if(canFix(phone))
System.out.println(": Fixed is "+fix(phone));
else
System.out.println(": is not fixable");
}
System.out.print("Enter a phone number: ");
phone=in.nextLine();
}
}
public static boolean isValid(String p)
{int n=0;
char c;
while(n<p.length())
{c=p.charAt(n);
switch(n)
{case 0:if(c!='(')
return false;
break;
case 4:if(c!=')')
return false;
break;
case 8:if(c!='-')
return false;
break;
default: if(!Character.isDigit(c))
return false;
}
n++;
}
return true;
}
public static boolean canFix(String p)
{int n=0,count=0;
while(n<p.length())
{
if(Character.isDigit(p.charAt(n)))
count++;
n++;
}
if(count==10)
return true;
else
return false;
}
public static String fix(String p)
{String result="";
char c;
int n=0,m=0;
while(n<p.length())
{c=p.charAt(n);
if(m==0)
{result=result+'(';
m++;
}
else if(m==4)
{result=result+')';
m++;
}
else if(m==8)
{result=result+'-';
m++;
}
if(Character.isDigit(c))
{result=result+c;
m++;
}
n++;
}
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.