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"