Reputation: 301
I'm newer to Java and I have a homework assignment in which I am supposed to write a program that converts a phone number to all possible strings associated with that number. I'm having a hard time figuring out the logic on this one though. This is what I have so far. Any suggestions would be appreciated.
import java.util.*;
public class phoneNumberString{
String[] thisString = {""};
public static void main(String[] args) {
}
public static String dialPad[][] = {
{"0"}, {"1"}, {"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"},
{"J", "K", "L"}, {"M", "N", "O"}, {"P", "Q", "R", "S"},
{"T", "U", "V"}, {"W", "X", "Y", "Z"}
};
public String[] dial(int number) {
for(int i = 0; i < dialPad.length; i++)
if(i == 0 || i == 1 )
thisString = new String [] {""};
return thisString;
}
public static void print(String[] thisString) {
System.out.println(thisString);
}
}
Upvotes: 0
Views: 102
Reputation: 36703
So the first thing to do is break the number down into its corresponding digits... i.e. 1234 is 1, 2, 3.... Various solutions, but easiest to understand without Math is via String manipulation.
private int[] numberAsDigits(int number) {
String numberAsString = String.valueOf(number);
int[] numberAsDigits = new int[numberAsString.length()];
char[] chars = numberAsString.toCharArray();
for (int i = 0; i < chars.length; i++) {
numberAsDigits[i] = Integer.parseInt(String.valueOf(chars[i]));
}
return numberAsDigits;
}
Now you need to interate through all the combinations... Say the number is "223"... your first combination would be AAD, and then BAD, then CAD, etc. When you get to CAD, your next will be ABD. To maintain your position you need an array of "counters" the same length as the number.
int[] counters = new int[numberAsDigits.length];
So you build your String from the counters
// current combination
StringBuilder builder = new StringBuilder();
for (int i = 0; i < counters.length; i++) {
String[] combosForCurrentDigit= dialPad[numberAsDigits[i]];
builder.append(combosForCurrentDigit[counters[i]]);
}
results.add(builder.toString());
The next bit is to increment the counters to the next combination. First add to the first, and then "ripple" or "carry over" the updates through, when they get to the end, reset and increment the next, i.e. the same logic as counting when you go from 99 to 100. Note we also need to include an end condition, this is when the last counter goes past the end of the array.
// increment the counters
counters[0]++;
for (int i = 0; i < counters.length; i++) {
String[] combosForCurrentDigit = dialPad[numberAsDigits[i]];
if (counters[i] == combosForCurrentDigit.length) {
counters[i] = 0;
if (i + 1 == counters.length) {
finished = true;
} else {
counters[i + 1]++;
}
}
}
Putting it all together
public static void main(String[] args) {
System.out.println(Arrays.toString(new phoneNumberString().dial(223)));
}
public static String dialPad[][] = { { "0" }, { "1" }, { "A", "B", "C" }, { "D", "E", "F" }, { "G", "H", "I" },
{ "J", "K", "L" }, { "M", "N", "O" }, { "P", "Q", "R", "S" }, { "T", "U", "V" }, { "W", "X", "Y", "Z" } };
public String[] dial(int number) {
int[] numberAsDigits = numberAsDigits(number);
int[] counters = new int[numberAsDigits.length];
boolean finished = false;
List<String> results = new ArrayList<>();
while (!finished) {
// current combination
StringBuilder builder = new StringBuilder();
for (int i = 0; i < counters.length; i++) {
String[] combosForCurrentDigit = dialPad[numberAsDigits[i]];
builder.append(combosForCurrentDigit[counters[i]]);
}
results.add(builder.toString());
// increment the counters
counters[0]++;
for (int i = 0; i < counters.length; i++) {
String[] combosForCurrentDigit = dialPad[numberAsDigits[i]];
if (counters[i] == combosForCurrentDigit.length) {
counters[i] = 0;
if (i + 1 == counters.length) {
finished = true;
} else {
counters[i + 1]++;
}
}
}
}
return results.toArray(new String[0]);
}
private int[] numberAsDigits(int number) {
String numberAsString = String.valueOf(number);
int[] numberAsDigits = new int[numberAsString.length()];
char[] chars = numberAsString.toCharArray();
for (int i = 0; i < chars.length; i++) {
numberAsDigits[i] = Integer.parseInt(String.valueOf(chars[i]));
}
return numberAsDigits;
}
Output
[AAD, BAD, CAD, ABD, BBD, CBD, ACD, BCD, CCD, AAE, BAE, CAE, ABE, BBE, CBE, ACE, BCE, CCE, AAF, BAF, CAF, ABF, BBF, CBF, ACF, BCF, CCF]
Upvotes: 1