Question: Update the following code so that it has no while(true) and breaks. Also fix the output as it is displayed wrong. text file: (356)153-9460 (290)261-7625
Update the following code so that it has no "while(true)" and "breaks". Also fix the output as it is displayed wrong.
text file:
(356)153-9460 (290)261-7625 (761)321-9457 (724)091-7819 (759)301-3133 (072)023-0203 (060)075-3782 (060)075-3782 (369)348-9660 (844)824-5853 (692)839-8466 (216)386-2922
desired output:
phone number: (356)153-9460 area code: 356 exchange: 153 line number: 9460 is free toll: false phone number: (290)261-7625 area code: 290 exchange: 261 line number: 7625 is free toll: false phone number: (761)321-9457 area code: 761 exchange: 321 line number: 9457 is free toll: false phone number: (724)091-7819 area code: 724 exchange: 091 line number: 7819 is free toll: false phone number: (759)301-3133 area code: 759 exchange: 301 line number: 3133 is free toll: false phone number: (072)023-0203 area code: 072 exchange: 023 line number: 0203 is free toll: false phone number: (060)075-3782 area code: 060 exchange: 075 line number: 3782 is free toll: false Duplicate phone number "(060)075-3782" discovered phone number: (369)348-9660 area code: 369 exchange: 348 line number: 9660 is free toll: false phone number: (844)824-5853 area code: 844 exchange: 824 line number: 5853 is free toll: true phone number: (692)839-8466 area code: 692 exchange: 839 line number: 8466 is free toll: false phone number: (216)386-2922 area code: 216 exchange: 386 line number: 2922 is free toll: false
Code to modify:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
public class PhoneNumber { private String num; public PhoneNumber(String num) { this.num=num; } / public String getAreaCode() { return num.substring(1, 4); } public String getExchange() { return num.substring(5, 8); } public String getLineNumber() { return num.substring(9); } public boolean isFreeToll() { if(getAreaCode().charAt(0)=='8') { return true; } return false; } public String toString() { return "phone number: "+num+" area code: "+getAreaCode()+" exchange: "+getExchange()+" line number: "+getLineNumber(); } public boolean equals(PhoneNumber phoneNum) { if(phoneNum.getAreaCode().compareTo(this.getAreaCode())==0 && phoneNum.getExchange().compareTo(this.getExchange())==0 && phoneNum.getLineNumber().compareTo(this.getLineNumber())==0) { return true; } return false; } public static String read(Scanner scan) { if(scan.hasNextLine()) { return scan.nextLine(); } return null; } public static void main(String[] args) throws FileNotFoundException { Scanner scan=new Scanner(new File("numbers.text")); PhoneNumber phoneNumNext,phoneNum = null; int counts=0; while(true) { String val=PhoneNumber.read(scan); if(val!=null) { phoneNum=new PhoneNumber(val); System.out.println(phoneNum+" is free toll: "+phoneNum.isFreeToll()); counts++; } else { break; } val=PhoneNumber.read(scan); if(val!=null) { phoneNumNext=new PhoneNumber(val); if(phoneNum.equals(phoneNumNext)) { System.out.println("Duplicate phone number \"("+phoneNumNext.getAreaCode()+")"+phoneNumNext.getExchange()+"-"+phoneNumNext.getLineNumber()+"\" discovered in the output"); } else { System.out.println(phoneNumNext+" is free toll: "+phoneNumNext.isFreeToll()); } counts++; } else { break; } } System.out.println(counts+" Phone numbers processed"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
