Hello, everyone. I'm new to this site so please pardon any newbie rules that I break with this post. Also, sorry about the way my code was inserted but I didn't see any way to post code snippets in the editor.
I realize that this particular problem has been around awhile but I've only seen it tackled in Java, not C#; thus, I'm bringing it to here in efforts to get some assistance on this one. Here's the problem....
Many companies use telephone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion:
A, B, and C = 2
D, E, and F = 3
G, H, and I = 4
J, K, and L = 5
M, N, and O = 6
P, Q, R, and S = 7
T, U, and V = 8
W, X, Y, and Z = 0
Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The program should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-GOOD the application should display 555-438-3663.
I'm actually running into two dilemmas with this one and my brain is mush. I've reached my limits in debugging experience so I'm hoping that someone can help me out here.
The first problem is, taking the example above as user input.....when I enter 555-Get-Food, I receive the "InvalidOperationException was unhandled" error message. In fact to be more exact, when I enter any number into the text box I get this error message and taking a look at the Locals window below I can see Value '5' and Type char letting me know that there's something going on with the numbers that I'm entering. (
Here should I just split the numbers out of my input and have the code focus only on the letters as a possible fix?)
So to investigate deeper, I find that when I enter all letters, for instance, GetGetFood this part of my code executes;
01 | private void numButton_Click(object sender, EventArgs e) |
05 | string input = numTextBox.Text; |
07 | if (IsValidNumber(input)) |
11 | //TelephoneFormat(ref input); |
12 | MessageBox.Show(" The number is " + Translate(input)); |
17 | MessageBox.Show("Invalid input"); |
however....instead of the letters being translated into numbers I receive the following message in my message box:
"The number is System.Int32[]"
Here's my code in it's entirety:
002 | using System.Collections.Generic; |
003 | using System.ComponentModel; |
008 | using System.Threading.Tasks; |
009 | using System.Windows.Forms; |
013 | namespace TelephoneNumberTranslator |
015 | public partial class TelephoneNumberTranslator : Form |
017 | public TelephoneNumberTranslator() |
019 | InitializeComponent(); |
023 | // The isValidNumber method accepts a string and returns true if it |
024 | // contains 10 digits, or false otherwise. |
025 | private bool IsValidNumber(string str) |
027 | const int VALID_LENGTH = 10; // Length of a valid string |
028 | bool valid = true; // Flag to indicate validity |
030 | // Check the string's length. |
031 | if (str.Length == VALID_LENGTH) |
042 | // The telephone format method accepts a string argument by reference and |
043 | // formats it as a telephone number. |
044 | private void TelephoneFormat(ref string str) |
046 | // First, insert the left paren at position 0. |
047 | str = str.Insert(0, "("); |
049 | // Next, insert the right paren at position 4. |
050 | str = str.Insert(4, ")"); |
052 | // Next, insert the hyphen at position 8. |
053 | str = str.Insert(8, "-"); |
056 | // Creates Dictionary to hold values |
057 | private readonly Dictionary<int, char[]> keys = |
058 | new Dictionary<int, char[]>() |
061 | {2, new[] {'a', 'b', 'c', 'A', 'B', 'C'}}, |
062 | {3, new[] {'d', 'D', 'E', 'e', 'f', 'F'}}, |
063 | {4, new[] {'g', 'G', 'H', 'h', 'I', 'i'}}, |
064 | {5, new[] {'j', 'J', 'K', 'k', 'L','l'}}, |
065 | {6, new[] {'m', 'M', 'N', 'n', 'O','o'}}, |
066 | {7, new[] {'p', 'P', 'Q', 'q', 'R', 'r', 'S','s'}}, |
067 | {8, new[] {'t', 'T', 'U', 'u', 'V','v'}}, |
068 | {9, new[] {'w', 'W', 'X', 'x', 'Y', 'y', 'Z', 'z'}}, |
073 | // Translates strings into numbers |
075 | public int[] Translate(string word) |
077 | return word.Select(c => |
078 | keys.First(k => k.Value.Contains(c)).Key).ToArray(); |
083 | private void numButton_Click(object sender, EventArgs e) |
086 | // Get a trimmed copy of the user's input. |
087 | string input = numTextBox.Text; |
089 | if (IsValidNumber(input)) |
093 | //TelephoneFormat(ref input); |
094 | MessageBox.Show(" The number is " + Translate(input)); |
099 | MessageBox.Show("Invalid input"); |
Mind you, this is my first semester programming in C# (Visual C#) and we're dealing with Processing Data. My initial thoughts were to try doing this by creating an enumerator to hold the numerical equivalents of the letters on a telephone dial pad but after I'd written it out I thought there had to be an easier way. Through searching I stumbled across Dictionary, did a bit of research on how to create it in C#, and as you see....tried my best using it. (That's probably where the majority of my trouble is so if anyone could show me how to use enums to accomplish this task more efficiently, being that I'm familiar with enumerators I'm open to that also.)
All in all, could someone please lend me a hand here? Thank you in advance.
"Q"
VulpesPosted Mar 19, 2014, 9:00 PM
Quintius WalkerPosted Mar 20, 2014, 11:26 AM
VulpesPosted Mar 20, 2014, 7:10 AM
input = input.Replace("-", "");
Quintius WalkerPosted Mar 19, 2014, 9:51 PM
Also, that method doesn't accept characters as arguments, i.e, input.Remove('-') won't work. It's throws the same exception with the error actually returning "555-get-food" as opposed to just the hyphen.
Even tried changing the user input example to 555getfood and formatting the output with TelephoneFormat method as such:
private void TelephoneFormat(ref string str)
{
// First, insert the left paren at position 0.
str = str.Insert(0, "(");
// Next, insert the right paren at position 4.
str = str.Insert(4, ")");
// Next, insert the hyphen at position 8.
str = str.Insert(8, "-");
}
which only puts me back at square one due to the fact that '(' isn't recognized as dictionary character.
There has to be a way to add '-' to the current dictionary somehow without creating a new dictionary of
Quintius WalkerPosted Mar 19, 2014, 7:32 PM
Taking your reply in consideration, I modify things a bit (although it's still a bit ugly)
Now, if let's say the user enters, 555getfood, the number correctly translates. However, there's one little problem left that I can't figure out. I've even tried:
char[] delim = { '-' } as a work around. If the user enters 555-get-food, I'm faced with the same InvalidOperation error that I was getting before I added numbers to my dictionary because there's no character in my dictionary representing the hyphen ('-') and I don't see where nor how I can add it?
Other than that it's working and translating properly.
Once again, thank you,
"Q"
Here's the rework by the way, sorry:
namespace TelephoneNumberTranslator
{
public partial class TelephoneNumberTranslator : Form
{
public TelephoneNumberTranslator()
{
InitializeComponent();
}
// The isValidNumber method accepts a string and returns true if it
// contains 10 digits, or false otherwise.
private bool IsValidNumber(string str)
{
const int VALID_LENGTH = 10; // Length of a valid string
bool valid = true; // Flag to indicate validity
// Check the string's length.
if (str.Length == VALID_LENGTH)
{
valid = true;
}
else
{
valid = false;
}
// Return the status
return valid;
}
// Creates Dictionary to hold values
private readonly Dictionary
new Dictionary
{
{1, new char[] {'1'}},
{2, new[] {'a', 'b', 'c', 'A', 'B', 'C', '2'}},
{3, new[] {'d', 'D', 'E', 'e', 'f', 'F', '3'}},
{4, new[] {'g', 'G', 'H', 'h', 'I', 'i', '4'}},
{5, new[] {'j', 'J', 'K', 'k', 'L','l', '5'}},
{6, new[] {'m', 'M', 'N', 'n', 'O','o', '6'}},
{7, new[] {'p', 'P', 'Q', 'q', 'R', 'r', 'S','s', '7'}},
{8, new[] {'t', 'T', 'U', 'u', 'V','v', '8'}},
{9, new[] {'w', 'W', 'X', 'x', 'Y', 'y', 'z', '9'}},
{0, new[] {' ', '0'}},
};
// Translates strings into numbers
public int[] Translate(string word)
{
return word.Select(c =>
keys.First(k => k.Value.Contains(c)).Key).ToArray();
}
private void numButton_Click(object sender, EventArgs e)
{
string input = numTextBox.Text;
if (IsValidNumber(input))
{
//TelephoneFormat(ref input);
MessageBox.Show(" The number is " + Translate(input)[0] + Translate(input)[1] + Translate(input)[2] + Translate(input)[3] + Translate(input)[4] + Translate(input)[5] + Translate(input)[6] + Translate(input)[7] + Translate(input)[8] + Translate(input)[9]);
}
else
{
MessageBox.Show("Invalid input");
}
}
}
}
VulpesPosted Mar 19, 2014, 12:50 PM
Consequently, this line:
MessageBox.Show(" The number is "+ Translate(input));
should be:
MessageBox.Show(" The number is "+ Translate(input)[0]);
Another point is that your Dictionary values only contain letters. They should also contain the numbers as well. So a key of 1 will map to a char array which contains '1' and so on.