This is my assignment....
Write a Java program which prompts the user for a telephone number (which doesn't have a dash or parenthesis) and output all the possible word combinations. For example, the phone number 1234567 will have as one of its combinations 1CFILOQ. Although your program is designed to handle 7 digit telephone numbers, it must be general enough to handle any length of input and output the appropriate combinations. Display the number of combinations at the end of input. If a number doesn't have letters associated with it (e.g. 1 and 0), simply output that number. Telephones have the following mapping:
Here's a sample given; the user types '123' and then '456'
Enter a phone number (numbers only). Type "exit" to end.
1AD
1AE
1AF
1BD
1BE
1BF
1CD
1CE
1CF
9 option(s) are available
Enter a phone number (numbers only). Type "exit" to end.
GJM
GJN
GJO
GKM
GKN
GKO
GLM
GLN
GLO
HJM
HJN
HJO
HKM
HKN
HKO
HLM
HLN
HLO
IJM
IJN
IJO
IKM
IKN
IKO
ILM
ILN
ILO
27 option(s) are available
My problem is i can't figure out how to find all the possible combinations, I've been working on it for hours and hours now. If anyone could help me out i'd probably love them. Even suggestions of how to find all the possible out comes using iteration would be useful, as i could convert that into recursion.
here's what i've got...
Write a Java program which prompts the user for a telephone number (which doesn't have a dash or parenthesis) and output all the possible word combinations. For example, the phone number 1234567 will have as one of its combinations 1CFILOQ. Although your program is designed to handle 7 digit telephone numbers, it must be general enough to handle any length of input and output the appropriate combinations. Display the number of combinations at the end of input. If a number doesn't have letters associated with it (e.g. 1 and 0), simply output that number. Telephones have the following mapping:
Here's a sample given; the user types '123' and then '456'
Enter a phone number (numbers only). Type "exit" to end.
1AD
1AE
1AF
1BD
1BE
1BF
1CD
1CE
1CF
9 option(s) are available
Enter a phone number (numbers only). Type "exit" to end.
GJM
GJN
GJO
GKM
GKN
GKO
GLM
GLN
GLO
HJM
HJN
HJO
HKM
HKN
HKO
HLM
HLN
HLO
IJM
IJN
IJO
IKM
IKN
IKO
ILM
ILN
ILO
27 option(s) are available
My problem is i can't figure out how to find all the possible combinations, I've been working on it for hours and hours now. If anyone could help me out i'd probably love them. Even suggestions of how to find all the possible out comes using iteration would be useful, as i could convert that into recursion.
here's what i've got...
Code:
import java.util.*; public class Telephone { private static Vector<String> recursion(Vector<String> p, int j) { int f = 1; for(int cc=0; cc < p.size(); cc++) //figured out the possible number of combinations f *= p.get(cc).length(); if(j < f) { for(int i=0; i < p.size(); i++) { if(p.get(i).charAt(0) == '1') System.out.print(p.get(i).charAt(0)); else if(p.get(i).charAt(0) == '0') System.out.print(p.get(i).charAt(0)); else System.out.print((p.get(i)).charAt(j)); } System.out.println(""); recursion(p,j+1); } return p; } public static String getOptions(String r) { Vector<String> x = new Vector<String>(); if(r.equals("exit")) //Says "bye" and then sees it self off... { System.out.println("Bye!"); System.exit(0); } for(int c=0;c < r.length(); c++) // Puts string in vector x.addElement(Character.toString(r.charAt(c))); Collections.replaceAll(x,"2","abc"); Collections.replaceAll(x,"3","def"); Collections.replaceAll(x,"4","ghi"); Collections.replaceAll(x,"5","jkl"); //Replaces numbers with their respective letters Collections.replaceAll(x,"6","mno"); Collections.replaceAll(x,"7","pqrs"); Collections.replaceAll(x,"8","tuv"); Collections.replaceAll(x,"9","wxyz"); recursion(x,0); // Call to the recursive method return r; } public static void main(String[] args) { Scanner Sc = new Scanner(System.in); System.out.println("Enter a phone number (numbers only). Type \"exit\" to end."); String b = Sc.nextLine(); System.out.println(getOptions(b)); System.out.println("option(s) are available"); Sc.close(); } }
Comment