View Full Version : Telephone Word Generator
davidc2
11-13-2006, 02:33 AM
I need something like this:
http://www.phonespelling.com/
Where I type a phone number and I get the list of possible words, I have been trying to think about the algorithm but I just surround myself on a sea of ifs... What can I try?
daniel_g
11-13-2006, 06:37 PM
Maybe if you post your "ifs" we migth be able to help you solve the problems your ideas may carry.
davidc2
11-13-2006, 07:26 PM
Maybe if you post your "ifs" we migth be able to help you solve the problems your ideas may carry.
Well I tried to think about it recursively, I'm first trying like if the input was a 2 digit number and the digits are 2 and 3, which would output A,B,C and D,E F respectively... like (this is just the pseudo)
number2 {A,B,C}
number3 {D,E,F}
method void x (Character ArrayOfDigits)
if digits.length == 1
if digit == 2
for j=0 to j<2
print number2[j]
return;
if digit == 3
for j=0 to j<2
print number3[j]
return;
else
if digit == 2
for j=0 to j<2
print number2[j]
x (ArrayOfDigits - leftMostDigit) //this would truncate the array
if digit == 3
for j=0 to j<2
print number3[j]
x (ArrayOfDigits - leftMostDigit)
But say the number was 23, then this would output (I think)
AD
E
F
BD
E
F
CD
E
F
right? Well I think that's what I'd do... I have no clue how to make it print for instance AD, AE, AF, BD, BE, BF....
My friend told me to try something like this 'cause the input is always going to be the same (this is for a 3 digit number)
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
//output here
}
}
}
And I get
000
001
002
010
011
012
etc.
Now how to convert that from letters to numbers, I guess using arrays, I have no clue :(
daniel_g
11-13-2006, 10:17 PM
What I would do, is create a bi-dimensional array with 10 columns and 4 rows. Each column would represent a number(0-9), and the rows would be the letters that correspond to that number. For example:
arr = [ -1, -1, A, D, G, J, M, P, T, W
-1, -1, B, E, H, K, N, Q, U, X
-1, -1, C, F, I, L, O, R, V, Y
-1, -1, -1, -1, -1, -1, -1, S, -1, Z]
-1 would just tell the code that the number doesn't have a letter.
And using the code your friend helped you create, print instead of the numbers, the elements on the array.
Ill take a closer look at your code when I come back home later tonigth.
davidc2
11-13-2006, 11:02 PM
The table I need to do has just 3 letters per number so something like:
char number2[] = { 'A', 'B', 'C' };
char number3[] = { 'D', 'E', 'F' };
char number4[] = { 'G', 'H', 'I' };
char number5[] = { 'J', 'K', 'L' };
char number6[] = { 'M', 'N', 'O' };
char number7[] = { 'P', 'R', 'S' };
char number8[] = { 'T', 'U', 'V' };
char number9[] = { 'W', 'X', 'Y' };
would work
daniel_g
11-14-2006, 04:29 AM
My friend told me to try something like this 'cause the input is always going to be the same (this is for a 3 digit number)
Code:
[code[
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
//output here
}
}
}[/code]
That's exactly rigth, for a one digit number you only need one loop. For an 8 digit number you would need 8 loops. That's going to look a bit nasty in the end, but if it works, it works. So what you want to do, is put this code inside you if/else staments so it looks something like:
int digit1 = 0;
int digit2 = 0;
...
int digit8 = 0;
if digits.length == 1
....
else if digits.length == 2;
....
else if digits.length == 3
digit1 = parseInt(charAt(1));
digit2 = parseInt(charAt(2));
digit3 = parseInt(chatAt(3));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
//output here
}
}
Before going any further, we need to create a bidimensional array. The arrays you created would work, but the bidimensional one will save you some work, plus it will help you avoid the problem of skipping characters(you skipped "Q" on char[] number7 :p ):
/*
* will create an array of 8 rows, 3 columns
* containing letters from 'A' to 'X'
* put this at the very top of your code inside your class
*/
char[][] number = new char[8][3];
int count = 65; //ASCII for 'A'
for(int i=0; i<8; i++){
for(int j=0; j<3; j++){
number[i][j] = (char)count;
count++;
//System.out.print(number[i][j]);//if you want to test, uncomment this
}
//System.out.println("");// if you want to test uncomment this
}
Now try to guess what the role digit1 - digit8 is going to play in the "//output here"
Hint: you will be using the bidirectional array I just created.
davidc2
11-14-2006, 05:21 AM
I got it working but I did a lot of stuff I wasn't supposed to do, but I'll do the 'good' code next... I just needed to have something working for tomorrow :)
/*
*The program inputs a 7 digit number from the user.
*It brakes the input into chracters of array phoneArray.
*It then compares each digit and accordingly assigns the
*propper Letter-array for that number.
*It then prints all possible combinations.
*/
import java.util.Scanner;
public class NumberToWord {
public static void main(String[] args) {
char number2[] = { 'A', 'B', 'C' };
char number3[] = { 'D', 'E', 'F' };
char number4[] = { 'G', 'H', 'I' };
char number5[] = { 'J', 'K', 'L' };
char number6[] = { 'M', 'N', 'O' };
char number7[] = { 'P', 'R', 'S' };
char number8[] = { 'T', 'U', 'V' };
char number9[] = { 'W', 'X', 'Y' };
char Letter0[] = { ' ',' ',' ' };
char Letter1[] = { ' ',' ',' ' };
char Letter2[] = { ' ',' ',' ' };
char Letter3[] = { ' ',' ',' ' };
char Letter4[] = { ' ',' ',' ' };
char Letter5[] = { ' ',' ',' ' };
char Letter6[] = { ' ',' ',' ' };
char Letter7[] = { ' ',' ',' ' };
Scanner scanner = new Scanner( System.in );
Character phoneArray[] = new Character[ 7 ];
System.out.print( "Enter a phone: " );
String phoneNumber = scanner.nextLine();
//Convert phoneNumber string to array of characters
for( int i=0; i < phoneNumber.length(); i++ ) {
phoneArray[i]=(new Character(phoneNumber.charAt(i)));
}
for (int i=0;i < phoneArray.length; i++) {
if ( phoneArray[i].equals('0') ){
System.out.println("Invalid Input (0)");
System.exit(1);
}
else if ( phoneArray[i].equals('1') ){
System.out.println("Invalid Input (1)");
System.exit(1);
}
}
//COMPARE FIRST DIGIT
if (phoneArray[0].equals('2'))
for (int j = 0; j < Letter0.length;j++)
Letter0[j] = number2[j];
else if (phoneArray[0].equals('3'))
for (int j = 0; j < Letter0.length;j++)
Letter0[j] = number3[j];
else if (phoneArray[0].equals('4'))
for (int j = 0; j < Letter0.length;j++)
Letter0[j] = number4[j];
else if (phoneArray[0].equals('5'))
for (int j = 0; j < Letter0.length;j++)
Letter0[j] = number5[j];
else if (phoneArray[0].equals('6'))
for (int j = 0; j < Letter0.length;j++)
Letter0[j] = number6[j];
else if (phoneArray[0].equals('7'))
for (int j = 0; j < Letter0.length;j++)
Letter0[j] = number7[j];
else if (phoneArray[0].equals('8'))
for (int j = 0; j < Letter0.length;j++)
Letter0[j] = number8[j];
else if (phoneArray[0].equals('9'))
for (int j = 0; j < Letter0.length;j++)
Letter0[j] = number9[j];
//COMPARE SECOND DIGIT
if (phoneArray[1].equals('2'))
for (int j = 0; j < Letter1.length;j++)
Letter1[j] = number2[j];
else if (phoneArray[1].equals('3'))
for (int j = 0; j < Letter1.length;j++)
Letter1[j] = number3[j];
else if (phoneArray[1].equals('4'))
for (int j = 0; j < Letter1.length;j++)
Letter1[j] = number4[j];
else if (phoneArray[1].equals('5'))
for (int j = 0; j < Letter1.length;j++)
Letter1[j] = number5[j];
else if (phoneArray[1].equals('6'))
for (int j = 0; j < Letter1.length;j++)
Letter1[j] = number6[j];
else if (phoneArray[1].equals('7'))
for (int j = 0; j < Letter1.length;j++)
Letter1[j] = number7[j];
else if (phoneArray[1].equals('8'))
for (int j = 0; j < Letter1.length;j++)
Letter1[j] = number8[j];
else if (phoneArray[1].equals('9'))
for (int j = 0; j < Letter1.length;j++)
Letter1[j] = number9[j];
//COMPARE THIRD DIGIT
if (phoneArray[2].equals('2'))
for (int j = 0; j < Letter2.length;j++)
Letter2[j] = number2[j];
else if (phoneArray[2].equals('3'))
for (int j = 0; j < Letter2.length;j++)
Letter2[j] = number3[j];
else if (phoneArray[2].equals('4'))
for (int j = 0; j < Letter2.length;j++)
Letter2[j] = number4[j];
else if (phoneArray[2].equals('5'))
for (int j = 0; j < Letter2.length;j++)
Letter2[j] = number5[j];
else if (phoneArray[2].equals('6'))
for (int j = 0; j < Letter2.length;j++)
Letter2[j] = number6[j];
else if (phoneArray[2].equals('7'))
for (int j = 0; j < Letter2.length;j++)
Letter2[j] = number7[j];
else if (phoneArray[2].equals('8'))
for (int j = 0; j < Letter2.length;j++)
Letter2[j] = number8[j];
else if (phoneArray[2].equals('9'))
for (int j = 0; j < Letter2.length;j++)
Letter2[j] = number9[j];
//COMPARE FORTH DIGIT
if (phoneArray[3].equals('2'))
for (int j = 0; j < Letter3.length;j++)
Letter3[j] = number2[j];
else if (phoneArray[3].equals('3'))
for (int j = 0; j < Letter3.length;j++)
Letter3[j] = number3[j];
else if (phoneArray[3].equals('4'))
for (int j = 0; j < Letter3.length;j++)
Letter3[j] = number4[j];
else if (phoneArray[3].equals('5'))
for (int j = 0; j < Letter3.length;j++)
Letter3[j] = number5[j];
else if (phoneArray[3].equals('6'))
for (int j = 0; j < Letter3.length;j++)
Letter3[j] = number6[j];
else if (phoneArray[3].equals('7'))
for (int j = 0; j < Letter3.length;j++)
Letter3[j] = number7[j];
else if (phoneArray[3].equals('8'))
for (int j = 0; j < Letter3.length;j++)
Letter3[j] = number8[j];
else if (phoneArray[3].equals('9'))
for (int j = 0; j < Letter3.length;j++)
Letter3[j] = number9[j];
//COMPARE FIFTH DIGIT
if (phoneArray[4].equals('2'))
for (int j = 0; j < Letter4.length;j++)
Letter4[j] = number2[j];
else if (phoneArray[4].equals('3'))
for (int j = 0; j < Letter4.length;j++)
Letter4[j] = number3[j];
else if (phoneArray[4].equals('4'))
for (int j = 0; j < Letter4.length;j++)
Letter4[j] = number4[j];
else if (phoneArray[4].equals('5'))
for (int j = 0; j < Letter4.length;j++)
Letter4[j] = number5[j];
else if (phoneArray[4].equals('6'))
for (int j = 0; j < Letter4.length;j++)
Letter4[j] = number6[j];
else if (phoneArray[4].equals('7'))
for (int j = 0; j < Letter4.length;j++)
Letter4[j] = number7[j];
else if (phoneArray[4].equals('8'))
for (int j = 0; j < Letter4.length;j++)
Letter4[j] = number8[j];
else if (phoneArray[4].equals('9'))
for (int j = 0; j < Letter4.length;j++)
Letter4[j] = number9[j];
//COMPARE SIXTH DIGIT
if (phoneArray[5].equals('2'))
for (int j = 0; j < Letter5.length;j++)
Letter5[j] = number2[j];
else if (phoneArray[5].equals('3'))
for (int j = 0; j < Letter5.length;j++)
Letter5[j] = number3[j];
else if (phoneArray[5].equals('4'))
for (int j = 0; j < Letter5.length;j++)
Letter5[j] = number4[j];
else if (phoneArray[5].equals('5'))
for (int j = 0; j < Letter5.length;j++)
Letter5[j] = number5[j];
else if (phoneArray[5].equals('6'))
for (int j = 0; j < Letter5.length;j++)
Letter5[j] = number6[j];
else if (phoneArray[5].equals('7'))
for (int j = 0; j < Letter5.length;j++)
Letter5[j] = number7[j];
else if (phoneArray[5].equals('8'))
for (int j = 0; j < Letter5.length;j++)
Letter5[j] = number8[j];
else if (phoneArray[5].equals('9'))
for (int j = 0; j < Letter5.length;j++)
Letter5[j] = number9[j];
//COMPARE SEVENTH DIGIT
if (phoneArray[6].equals('2'))
for (int j = 0; j < Letter6.length;j++)
Letter6[j] = number2[j];
else if (phoneArray[6].equals('3'))
for (int j = 0; j < Letter6.length;j++)
Letter6[j] = number3[j];
else if (phoneArray[6].equals('4'))
for (int j = 0; j < Letter6.length;j++)
Letter6[j] = number4[j];
else if (phoneArray[6].equals('5'))
for (int j = 0; j < Letter6.length;j++)
Letter6[j] = number5[j];
else if (phoneArray[6].equals('6'))
for (int j = 0; j < Letter6.length;j++)
Letter6[j] = number6[j];
else if (phoneArray[6].equals('7'))
for (int j = 0; j < Letter6.length;j++)
Letter6[j] = number7[j];
else if (phoneArray[6].equals('8'))
for (int j = 0; j < Letter6.length;j++)
Letter6[j] = number8[j];
else if (phoneArray[6].equals('9'))
for (int j = 0; j < Letter6.length;j++)
Letter6[j] = number9[j];
//PRINT
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++) {
System.out.print(Letter0[i]);
System.out.print(" ");
System.out.print(Letter1[j]);
System.out.print(" ");
System.out.print(Letter2[k]);
System.out.print(" ");
System.out.print(Letter3[l]);
System.out.print(" ");
System.out.print(Letter4[m]);
System.out.print(" ");
System.out.print(Letter5[n]);
System.out.print(" ");
System.out.print(Letter6[o]);
System.out.println();
}
}
}
}
} //end k for
} //end j for
} // end i for
}
}
Aradon
11-14-2006, 06:02 PM
There are three things that I think you could use to clean up the code a little bit. The first is optional however.
1) Enums (http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html)
2) Switch (http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html) Statement w/ loop
3) Recursion (http://en.wikipedia.org/wiki/Recursion#Recursion_in_computer_science)
Enums would allow you to easilly map letters and numbers. this is optional with the arrays.
A Switch statement would very eaisly allow you to thin out your if / else if's into a smaller statement.
And recursion (with a lot of thinking and banging of the head) would allow you to create a smaller and more eloquent way of doing it.
Presently, if the code works, I wouldn't mess with it. Especially if it's an assignment. But if you'd like to dive into the fun filled world of learning, I would think about trying to do those three things to increase your knowledge of the language and of basic computer science theory.
Just make sure to make a copy and do the editing on the copy ;)
Learning is fun! :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.