In: Computer Science
Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them (as seen below). Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use this tool to develop the seven-letter word “NUMBERS.” 2: A B C 3: D E F 4: G H I 5: J K L 6: M N 0 7: P R S 8: T U V 9: W X Y Every seven-letter phone number corresponds to many different seven-letter words, but most of these words represent unrecognizable juxtapositions of letters. It’s possible, however, that the owner of a barbershop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be pleased to know that the number corresponds to the letters “PETCARE.” An automotive dealership would be pleased to know that the dealership number, 639-2277, corresponds to “NEWCARS.” Write a program that prompts the user to enter a seven-digit telephone number as input and calculates all possible seven-letter word combinations. After sorting the result, print the first and last 10 combinations.
Please include pictures of compilation
//PhoneNumber.java
import java.io.*;
import java.util.Scanner;
public class PhoneNumber {
public static void main(String[] args)
{
// Scanner class
Scanner input = new Scanner(System.in);
// Array to store the digit
Character phoneNumbersInDigits[] = new Character[7];
// user input
String userNumber;
// Create one-dimensional arrays for letters according to digits
except 0 and 1
char[] two = { 'A', 'B', 'C' };
char[] three = { 'D', 'E', 'F' };
char[] four = { 'G', 'H', 'I' };
char[] five = { 'J', 'K', 'L' };
char[] six = { 'M', 'N', 'O' };
char[] seven = { 'P', 'R', 'S' };
char[] eight = { 'T', 'U', 'V' };
char[] nine = { 'W', 'X', 'Y' };
// Create one-dimensional arrays to store the letters according to
the
// digits position
char charAt0[] = new char[3];
char charAt1[] = new char[3];
char charAt2[] = new char[3];
char charAt3[] = new char[3];
char charAt4[] = new char[3];
char charAt5[] = new char[3];
char charAt6[] = new char[3];
// Initialize a count variable to count the number of phone
numbers as strings
int count = 0;
// user to enter the phone number
System.out.print("Enter Phone Number:");
userNumber = input.nextLine();
//check user input a 0 or 1 and to check the dash after the third
digit
for (int i = 0; i < userNumber.length(); i++) {
if (i != 3) {
if (!Character.isDigit(userNumber.charAt(i))) {
System.out.println("Invalid phone number format ###-####, # is a
digit");
System.exit(0);
}
}
if (userNumber.charAt(i) == '0' || userNumber.charAt(i) ==
'1'
|| userNumber.charAt(3) != '-') {
System.out.println("Invalid phone number with 0 or 1 or without
'-' in correct position");
System.exit(0);
}
}
int index = 0;
for (int i = 0; i < userNumber.length(); i++) {
if (i == 3)
continue;
else {
phoneNumbersInDigits[index] = userNumber.charAt(i);
index++;
}
}
// Compare the first digit of user entered phone number and copy
the elements into another array
if (phoneNumbersInDigits[0].equals('2'))
System.arraycopy(two, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('3'))
System.arraycopy(three, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('4'))
System.arraycopy(four, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('5'))
System.arraycopy(five, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('6'))
System.arraycopy(six, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('7'))
System.arraycopy(seven, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('8'))
System.arraycopy(eight, 0, charAt0, 0, charAt0.length);
else if (phoneNumbersInDigits[0].equals('9'))
System.arraycopy(nine, 0, charAt0, 0, charAt0.length);
// Compare the second digit of user entered phone number and copy
the elements into another array
if (phoneNumbersInDigits[1].equals('2'))
System.arraycopy(two, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('3'))
System.arraycopy(three, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('4'))
System.arraycopy(four, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('5'))
System.arraycopy(five, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('6'))
System.arraycopy(six, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('7'))
System.arraycopy(seven, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('8'))
System.arraycopy(eight, 0, charAt1, 0, charAt1.length);
else if (phoneNumbersInDigits[1].equals('9'))
System.arraycopy(nine, 0, charAt1, 0, charAt1.length);
// Compare the third digit of user entered phone number and copy
the elements into another array
if (phoneNumbersInDigits[2].equals('2'))
System.arraycopy(two, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('3'))
System.arraycopy(three, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('4'))
System.arraycopy(four, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('5'))
System.arraycopy(five, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('6'))
System.arraycopy(six, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('7'))
System.arraycopy(seven, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('8'))
System.arraycopy(eight, 0, charAt2, 0, charAt2.length);
else if (phoneNumbersInDigits[2].equals('9'))
System.arraycopy(nine, 0, charAt2, 0, charAt2.length);
// No need to compare 4th digits as its the '-'
// Compare the fifth digit of user entered phone number and copy
the elements into another array
if (phoneNumbersInDigits[3].equals('2'))
System.arraycopy(two, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('3'))
System.arraycopy(three, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('4'))
System.arraycopy(four, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('5'))
System.arraycopy(five, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('6'))
System.arraycopy(six, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('7'))
System.arraycopy(seven, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('8'))
System.arraycopy(eight, 0, charAt3, 0, charAt3.length);
else if (phoneNumbersInDigits[3].equals('9'))
System.arraycopy(nine, 0, charAt3, 0, charAt3.length);
// Compare the sixth digit of user entered phone number and copy
the elements into another array
if (phoneNumbersInDigits[4].equals('2'))
System.arraycopy(two, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('3'))
System.arraycopy(three, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('4'))
System.arraycopy(four, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('5'))
System.arraycopy(five, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('6'))
System.arraycopy(six, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('7'))
System.arraycopy(seven, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('8'))
System.arraycopy(eight, 0, charAt4, 0, charAt4.length);
else if (phoneNumbersInDigits[4].equals('9'))
System.arraycopy(nine, 0, charAt4, 0, charAt4.length);
// Compare the seventh digit of user entered phone number and copy
the elements into another array
if (phoneNumbersInDigits[5].equals('2'))
System.arraycopy(two, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('3'))
System.arraycopy(three, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('4'))
System.arraycopy(four, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('5'))
System.arraycopy(five, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('6'))
System.arraycopy(six, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('7'))
System.arraycopy(seven, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('8'))
System.arraycopy(eight, 0, charAt5, 0, charAt5.length);
else if (phoneNumbersInDigits[5].equals('9'))
System.arraycopy(nine, 0, charAt5, 0, charAt5.length);
// Compare the eighth digit of user entered phone number and copy
the elements into another array
if (phoneNumbersInDigits[6].equals('2'))
System.arraycopy(two, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('3'))
System.arraycopy(three, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('4'))
System.arraycopy(four, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('5'))
System.arraycopy(five, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('6'))
System.arraycopy(six, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('7'))
System.arraycopy(seven, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('8'))
System.arraycopy(eight, 0, charAt6, 0, charAt6.length);
else if (phoneNumbersInDigits[6].equals('9'))
System.arraycopy(nine, 0, charAt6, 0, charAt6.length);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
for (int m = 0; m < 3; m++) {
for (int n = 0; n < 3; n++) {
for (int o = 0; o < 3; o++) {
if (count < 10 || count > 2176) {
System.out.print(count + ": ");
System.out.print(charAt0[i]);
System.out.print(charAt1[j]);
System.out.print(charAt2[k]);
System.out.print(charAt3[l]);
System.out.print(charAt4[m]);
System.out.print(charAt5[n]);
System.out.print(charAt6[o]);
System.out.print('\n');
}
if (count == 2174)
System.out.println("...");
count++;
}
}
}
}
}
}
}
}
}
OUTPUT :