Check numbers with characters like a phone keypad? Help!
I need to create a program that will compare a list of words from a text file to a telephone number in another text file. For example, given the telephone number 416 465 9355 then it would be 416 INK WELL. (two separate words and the area code is excluded.) This is what I've managed to get so far:
Code :
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;
}
}
The array for the results is always null. I've been doing this for hours and I have no clue!
Re: Check numbers with characters like a phone keypad? Help!
Which array(s) are you talking about? Can you list the names of the arrays that are null?
Does your code have the same variables defined locally and as class variables? The local variables will shadow the class variables so the class variables do NOT get values.