Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Check numbers with characters like a phone keypad? Help!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

Similar Threads

  1. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. [SOLVED] Strings and Characters
    By av8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 4th, 2011, 05:39 PM
  3. Replies: 4
    Last Post: November 14th, 2010, 05:02 PM
  4. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM
  5. Check for palindrome numbers
    By SnooSnoo in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 4th, 2010, 06:11 PM