import java.util.*;
import java.io.*;
public class Demo {
public static void main (String[] args) throws FileNotFoundException{
char[][] phonePad = new char[10][4];
String[] phoneNumber;
String[] sevenCharWords = new String[2000000];
String[] threeCharWords = new String[2000000];
String[] fourCharWords = new String[2000000];
String[] sevenWordResults;
String [] threeCharResults = new String[20];
String [] fourCharResults = new String[20];
initialize(phonePad);
phoneNumber = readNumbers();
readWords(sevenCharWords, threeCharWords, fourCharWords);
numWordCheck (phoneNumber, threeCharWords, fourCharWords, phonePad, threeCharResults, fourCharResults);
}
public static void initialize (char[][] matrix) {
char term = 'A';
int i, j;
for (i = 2; i < matrix.length; i++) {
if (i==7 || i==9) {
for (j = 0; j < matrix[0].length; j++) {
matrix[i][j] = term;
term++;
}
}
else {
for (j = 0; j < matrix[0].length-1; j++) {
matrix[i][j] = term;
term++;
}
}
}
}
public static String[] readNumbers () throws FileNotFoundException {
String[] numbers = new String [100];
File readIn = new File ("tel.txt");
Scanner fileReader = new Scanner (readIn);
int i = 0;
String num = "";
while (fileReader.hasNext()) {
numbers[i] = fileReader.nextLine();
i += 1;
}
fileReader.close();
if (i < numbers.length) {
num = String.valueOf(i);
numbers[numbers.length-1] = num;
numbers = trimArray(numbers);
}
return numbers;
}
public static String[] trimArray(String[] toTrim) {
int length = Integer.parseInt(toTrim[toTrim.length-1]);
String[] results = new String[length];
for (int i = 0; i < length; i++) {
results[i] = toTrim[i];
}
return results;
}
public static void readWords (String[] sevenCharWords, String[] threeCharWords, String[] fourCharWords) throws FileNotFoundException {
String temp = "";
File readIn = new File ("smallWords.txt");
Scanner fileReader = new Scanner (readIn);
int i = 0, j=0, k=0;
while (fileReader.hasNextLine()) {
temp = fileReader.nextLine();
if (temp.length() == 7) {
sevenCharWords[i] = temp;
i += 1;
}
else if (temp.length() == 3) {
threeCharWords[j] = temp;
j += 1;
}
else if (temp.length() == 4) {
fourCharWords[k] = temp;
k += 1;
}
}
fileReader.close();
}
public static void numWordCheck (String[] phoneNumbers, String[] threeCharWords, String[] fourCharWords, char[][] phonePad, String [] threeCharResults, String [] fourCharResults) {
int i, j, k, p = 0, count;
int pos;
boolean match = false;
for (i=0; i<phoneNumbers.length; i++) {
clearArray(threeCharResults);
clearArray(fourCharResults);
k=0;
if (phoneNumbers[i] != null) {
String firstThree = phoneNumbers[i].substring(4,7);
String lastFour = phoneNumbers[i].substring(7);
for (j=0; j < threeCharResults.length; j++) {
if (threeCharWords[j] != null)
match = smallCheckMatch(firstThree, threeCharWords[j], phonePad);
if (match == true) {
threeCharResults[k] = threeCharWords[j];
k += 1;
}
}
for (j=0, count=0; j < fourCharResults.length; j++, count++) {
if (fourCharWords[j] != null)
match = smallCheckMatch(lastFour, fourCharWords[j], phonePad);
if (match == true) {
fourCharResults[k] = fourCharWords[j];
k += 1;
}
}
}
}
}
public static void clearArray (String[] toClear) {
for (int i=0; i <toClear.length; i++) {
toClear[i] = null;
}
}
public static boolean smallCheckMatch (String phoneNumberPortion, String word, char[][] phonePad) {
int i = 0, k = 0;
int num;
boolean continueCheck = true;
while (continueCheck == true && i < word.length()) {
num = (int) phoneNumberPortion.charAt(i) - '0';
if (num == 7 || num == 9) {
if (word.charAt(i) == phonePad[num][k] || word.charAt(i) == phonePad[num][k+1] || word.charAt(i) == phonePad[num][k+2] || word.charAt(i) == phonePad[num][k+3]) {
continueCheck = true;
i += 1;
}
else {
continueCheck = false;
}
}
else {
if (word.charAt(i) == phonePad[num][k] || word.charAt(i) == phonePad[num][k+1] || word.charAt(i) == phonePad[num][k+2]) {
continueCheck = true;
i += 1;
}
else {
continueCheck = false;
}
}
}
if (continueCheck == true)
return true;
else
return false;
}
}