Java For this assignment question, write a program which will get the personal a
ID: 3721185 • Letter: J
Question
Java
For this assignment question, write a program which will get the personal alphabetic telephone numbers from a text file and store them in an array list called “alphabeticPhoneNumbers”. If the number of the characters is not between 12 and 14, inclusively and there is any other character besides “a-z” or “A-Z”, or “-“, or “0-9” in a record, those should be flagged as ill-defined data. This ill-defined data needs to remove from the original array list and move it to another array list called “errorProneNumbers”. In other words, error involving records should be saved in a separate array list. If there no error in the data, it needs to be translated to its numeric equivalent to store it in another array list that is called ‘equivalentPhoneNumber’.
At the end application should display the array list that has the telephone number with alphabetic characters, and the list that has their numeric equivalent, and the list that has data (alphabetic phone number) with error.
For example, when the program reads 555-GET-FOOD from the original list, its equivalent 555-438-3663 needs to be stored into the list ‘equivalentPhoneNumber’. When it reads 222-ASK-???? from the original list, this error prone data needs to move to the list ‘errorProneNumbers’.
On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion;
A, B and C = 2
D, E, and F = 3
G, H, and I = 4
J, K, and L = 5
M, N, and O = 6
P, Q, R, and S = 7
T, U, and V = 8
W, X, Y, and Z = 9
Hint: Your program should display the result as shown in below
Original Data with no-error:
[1-800-FLOWERS 1-800-GOT-JUNK 555-GET-FOOD 1-800-PET-MEDS 1-800-LAWYERS 1-800-GO-FONTS 713-333-MOVE]
Numeric Phone Numbers:
[1-800-3569377 1-800-468-5865 555-438-3663 1-800-738-6337 1-800-5299377 1-800-46-36687 713-333-6683]
Error Prone Alphabetic Numbers:
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PhoneDirectory {
static Map<Character, Integer> map=new HashMap<>();
public PhoneDirectory() {
map.put('A', 2);
map.put('B', 2);
map.put('C', 2);
map.put('D', 3);
map.put('E', 3);
map.put('F', 3);
map.put('G', 4);
map.put('H', 4);
map.put('I', 4);
map.put('J', 5);
map.put('K', 5);
map.put('L', 5);
map.put('M', 6);
map.put('N', 6);
map.put('O', 6);
map.put('P', 7);
map.put('Q', 7);
map.put('R', 7);
map.put('S', 7);
map.put('T', 8);
map.put('U', 8);
map.put('V', 8);
map.put('W', 9);
map.put('X', 9);
map.put('Y', 9);
map.put('Z', 9);
}
public static void print(List<String> list) {
System.out.print("[");
for (String string : list) {
System.out.print(string+" ");
}
System.out.println("]");
}
public static void main(String[] args) {
/*
* input file directory.txt format
* 1-800-FLOWERS
1-800-GOT-JUNK
555-GET-FOOD
1-800-PET-MEDS
1-800-LAWYERS
1-800-GO-FONTS
713-333-MOVE((ABC
*
*/
PhoneDirectory directory=new PhoneDirectory();
String path="C:\Users\Nasir Bhat\eclipse-workspace\WeatherForecast\src\grade\directory.txt";
BufferedReader reader=null;
try {
reader=new BufferedReader(new FileReader(path));
String readLine=null;
List<String> alphabeticPhoneNumbers=new ArrayList<>();
List<String> errorProneNumbers=new ArrayList<>();
List<String> equivalentPhoneNumber=new ArrayList<>();
// System.out.println(reader);
while ((readLine = reader.readLine()) != null) {
readLine=readLine.replace("-", "");
readLine=readLine.toUpperCase();
// System.out.println(readLine.length()+" "+readLine.matches("[A-Z0-9]*"));
if(readLine.length()<12 || readLine.length()>14)
errorProneNumbers.add(readLine);
else if(!readLine.matches("[A-Z0-9]*")) {
errorProneNumbers.add(readLine);
}
if(readLine.matches("[A-Z0-9]*")) {
alphabeticPhoneNumbers.add(readLine);
}
if(readLine.matches("[A-Z0-9]*")) {
String equivalent="";
for(int i=0;i<readLine.length();i++) {
if(Character.isAlphabetic(readLine.charAt(i))) {
equivalent+=map.get(readLine.charAt(i));
}
else if(Character.isDigit(readLine.charAt(i))) {
equivalent+=readLine.charAt(i);
}
}
equivalentPhoneNumber.add(equivalent);
}
}
System.out.println("Original Data with no-error:");
print(alphabeticPhoneNumbers);
System.out.println("Numeric Phone Numbers:");
print(equivalentPhoneNumber);
System.out.println("Error Prone Alphabetic Numbers:");
print(errorProneNumbers);
}catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
finally {
if(reader!=null)
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.