Question: I am currenty working on a C# program, but my ending display does not have the correct corresponding numeric value to the alphabetic values entered.
I am currenty working on a C# program, but my ending display does not have the correct corresponding numeric value to the alphabetic values entered. Any clue as to what code is incorrect?
Purpose: Create an application that lets the user enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number ith any alphabetic characters that appeared in the original translated to their numeric equivalent. Use the numeric/alphabetic mapping: 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, and R = 7 S, T, and U = 8 W, X, Y, and Z = 9 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace AlphabeticTelephoneNumberTranslatorHW27 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void clearButton_Click(object sender, EventArgs e) { teleNumberTxtBox.Clear(); translatedLabel.Text = ""; }
private void translateButton_Click(object sender, EventArgs e) { //trim entered 10-character telephone number string charNum = teleNumberTxtBox.Text.Trim(); //use method to check if correct format if(isCorrect(charNum)) { translatedLabel.Text = translateNumber(charNum); } else { MessageBox.Show("ERROR! Enter valid telephone number format!"); }
}
//method to check if format is correct private bool isCorrect(string cN) { const int RequiredLength = 12; string input = cN;
if (input.Length == RequiredLength) { //checks for '-' char in respected positions and returns true if correct if (input.Substring(3, 1) == "-" && input.Substring(7, 1) == "-") { //delete hyphens from user input input = input.Remove(3, 1); input = input.Remove(6, 1); foreach (char ch in input) { //make sure all input is either letter or digit and return false if not if (!char.IsLetterOrDigit(ch)) { return false; } } } } else { //return false if '-' is not in correct position return false; } return true; }
//method to convert character telephone number into numeric counterpart private string translateNumber(string cn) { //variable to hold translated number string translated = string.Empty;
//using a dictionary collect to hold corresponding number/alphabetic value Dictionary
obj1.Add("ABC", '2'); obj1.Add("DEF", '3'); obj1.Add("GHI", '4'); obj1.Add("JKL", '5'); obj1.Add("MNO", '6'); obj1.Add("PQRS",'7'); obj1.Add("TUV",'8'); obj1.Add("WXYZ", '9');
KeyValuePair
foreach (char ch in cn) { char cChar = char.ToUpper(ch); if(char.IsLetter(cChar)) { getKeyValuePair = obj1.FirstOrDefault(X => X.Key.Contains(char.ToUpper(ch)));
translated += getKeyValuePair.Value.ToString(); } else { translated += cChar.ToString(); }
}
return translated; } } }

555-GET-FOOD is displaying: 555-525156-51545451
Alphabetic Telephone Number Translator Enter the telephone number is XXX-XXX-XXXX format: Translate Number Clear
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
