Quintius Walker

Quintius Walker

  • NA
  • 29
  • 5.2k

Telephone translator problem: InvalidOperation error/ System

Mar 19 2014 10:41 AM
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;
01private void numButton_Click(object sender, EventArgs e)
02       {
03 
04           .
05           string input = numTextBox.Text;
06            
07           if (IsValidNumber(input))
08           {
09 
10                
11               //TelephoneFormat(ref input);
12               MessageBox.Show(" The number is " + Translate(input));
13               
14           }
15           else
16           {
17               MessageBox.Show("Invalid input");
18           }
19       }


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:
001using System;
002using System.Collections.Generic;
003using System.ComponentModel;
004using System.Data;
005using System.Drawing;
006using System.Linq;
007using System.Text;
008using System.Threading.Tasks;
009using System.Windows.Forms;
010 
011 
012 
013namespace TelephoneNumberTranslator
014{
015    public partial class TelephoneNumberTranslator : Form
016    {
017        public TelephoneNumberTranslator()
018        {
019            InitializeComponent();
020        }
021 
022 
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)
026        {
027            const int VALID_LENGTH = 10; // Length of a valid string
028            bool valid = true// Flag to indicate validity
029 
030            // Check the string's length.
031            if (str.Length == VALID_LENGTH)
032            {
033                valid = true;
034            }
035            else
036            {
037                valid = false;
038            }
039            // Return the status
040            return valid;
041        }
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)
045        {
046            // First, insert the left paren at position 0.
047            str = str.Insert(0, "(");
048 
049            // Next, insert the right paren at position 4.
050            str = str.Insert(4, ")");
051 
052            // Next, insert the hyphen at position 8.
053            str = str.Insert(8, "-");
054        }
055 
056        // Creates Dictionary to hold values
057        private readonly Dictionary<int, char[]> keys =
058            new Dictionary<int, char[]>()
059                {
060                    {1, new 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'}},
069                    {0, new[] {' '}},
070 
071                };
072 
073        // Translates strings into numbers
074 
075        public int[] Translate(string word)
076        {
077            return word.Select(c =>
078                keys.First(k => k.Value.Contains(c)).Key).ToArray();
079 
080              
081        }
082 
083        private void numButton_Click(object sender, EventArgs e)
084        {
085 
086            // Get a trimmed copy of the user's input.
087            string input = numTextBox.Text;
088             
089            if (IsValidNumber(input))
090            {
091 
092                 
093                //TelephoneFormat(ref input);
094                MessageBox.Show(" The number is " + Translate(input));
095                
096            }
097            else
098            {
099                MessageBox.Show("Invalid input");
100            }
101        }
102    }
103}


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"

Answers (6)