This Must Be Done In C Alphabetic Telephone Number Translato
This Must Be Done In C#
Alphabetic Telephone Number Translator 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 = 9
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 with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663.
I looked through the textbook answers, and I currently have the code below, but it throws errors.
Dictionary and KeyValuePair both throw errors and say<TKey,Tvalue> requires 2 arguements.
How can i fix this so that this code will work?? the chegg textbook answer i am trying to use is located here
https://www.chegg.com/homework-help/Starting-out-with-Visual-C-2012-3rd-edition-chapter-8-problem-9PP-solution-9780133250596
i have fumbled around and tried and tried to make this work, i do not know what\'s wrong with it.
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 alphabeticphone
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool isvalidformat(string input)
{
const int valid_length = 12;
string strinput = input;
if (strinput.Length == valid_length)
{
if (strinput.Substring(3,1) == \"-\" && strinput.Substring(7,1) == \"-\")
{
strinput = strinput.Remove(3, 1);
strinput = strinput.Remove(6, 1);
foreach (char ch in strinput)
{
if (!char.IsLetterOrDigit(ch))
{
return false;
}
}
}
}
else
{
return false;
}
return true;
}
private string encrypttelephonenumber(string input)
{
string encrypt = string.Empty;
Dictionary dictEncrypt = new Dictionary();
dictEncrypt.Add(\"ABC\", \'2\');
dictEncrypt.Add(\"DEF\", \'3\');
dictEncrypt.Add(\"GHI\", \'4\');
dictEncrypt.Add(\"JKL\", \'5\');
dictEncrypt.Add(\"MNO\", \'6\');
dictEncrypt.Add(\"PQRS\", \'7\');
dictEncrypt.Add(\"TUV\", \'8\');
dictEncrypt.Add(\"WXYZ\", \'9\');
KeyValuePair getDictKeyValuePair = new KeyValuePair();
foreach (char ch in input)
{
char cChar = char.ToUpper(ch);
if(char.IsLetter(cChar))
{
getDictKeyValuePair.Value.ToString();
}
else
{
encrypt += cChar.ToString();
}
}
return encrypt;
}
private void button1_Click(object sender, EventArgs e)
{
string input = userinput.Text.Trim();
if (isvalidformat(input))
{
userinput.Text = encrypttelephonenumber(input);
}
else
{
MessageBox.Show(\"input a valid phone number.\");
}
}
private void exitbtn_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Solution
Hello, i have checked the code and identified that it has some wrong implementation for Generic Dictionary class.
The reason you are getting the error of \"<TKey,Tvalue> requires 2 arguments\" is because you are attempting to use the Dictionary<TKey, TValue> class which requires generic type arguments and there is no Dictionary class which does not require generic type arguments.
When using a class which uses generic type arguments you are required to supply the types you want to use whenever you declare or instantiate a variable dealing with the class. And above during declaration of dictionary class object, the generic type arguments are not supplied (which is WRONG).
Below is the small example for the dictionary class for more clarity:
*******************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace DictionarySample
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();
AuthorList.Add(\"Hemant Gupta\", 35);
AuthorList.Add(\"Russel Brooke\", 25);
AuthorList.Add(\"JK Rowling\", 29);
AuthorList.Add(\"Mia Gold\", 21);
AuthorList.Add(\"Dinesh Singh\", 84);
// Read all data
Console.WriteLine(\"Authors List\");
foreach( KeyValuePair<string, Int16> author in AuthorList )
{
Console.WriteLine(\"Key = {0}, Value = {1}\",
author.Key, author.Value);
}
Console.ReadKey();
}
}
}
**********************************************************************************************
The modification needed in your code is as below (changes are highlighted in BOLD):
private string encrypttelephonenumber(string input)
{
string encrypt = string.Empty;
Dictionary<string, Int16> dictEncrypt = new Dictionary<string, Int16>();
dictEncrypt.Add(\"ABC\", \'2\');
dictEncrypt.Add(\"DEF\", \'3\');
dictEncrypt.Add(\"GHI\", \'4\');
dictEncrypt.Add(\"JKL\", \'5\');
dictEncrypt.Add(\"MNO\", \'6\');
dictEncrypt.Add(\"PQRS\", \'7\');
dictEncrypt.Add(\"TUV\", \'8\');
dictEncrypt.Add(\"WXYZ\", \'9\');
KeyValuePair<string, Int16> getDictKeyValuePair = new KeyValuePair<string, Int16>();
foreach (char ch in input)
{
char cChar = char.ToUpper(ch);
if(char.IsLetter(cChar))
{
getDictKeyValuePair.Value.ToString();
}
else
{
encrypt += cChar.ToString();
}
}
return encrypt;
}



