I need help on my C# Programming Homework. Here is the Problem: Many companies u
ID: 3687915 • Letter: I
Question
I need help on my C# Programming Homework. Here is 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 = 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.
My Program when entering 555-GET-FOOD. However, entering 815-GET-FOOD into my program gives me an exception "Specified argument was out of the range of valid values." Can anyone help me fix this code without changing too much? I've spent hours trying.
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 NumberTranslate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
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;
}
private void TelephoneFormat(ref string str)
{
// Next, insert the hyphen at position 4.
str = str.Insert(3, "-");
//Next, insert the hypen at position 6.
str = str.Insert(7, "-");
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void convertButton_Click(object sender, EventArgs e)
{
// Get a trimmed copy of the user's input.
string input = textBox1.Text;
string endInput = "";
input = input.Trim();
input = input.Replace("-", "");
input = input.Replace(")", "");
input = input.Replace("(", "");
if (IsValidNumber(input))
{
for (int counter = 0; counter < 10; counter = counter + 1)
{
if (input[counter] == 'A' || input[counter] == 'B' || input[counter] == 'C' || input[counter] == '2')
{
endInput = endInput.Insert(counter, "2");
}
else if (input[counter] == 'D' || input[counter] == 'E' || input[counter] == 'F' || input[counter] == '3')
{
endInput = endInput.Insert(counter, "3");
}
else if (input[counter] == 'G' || input[counter] == 'H' || input[counter] == 'I' || input[counter] == '4')
{
endInput = endInput.Insert(counter, "4");
}
else if (input[counter] == 'J' || input[counter] == 'K' || input[counter] == 'L' || input[counter] == '5')
{
endInput = endInput.Insert(counter, "5");
}
else if (input[counter] == 'M' || input[counter] == 'N' || input[counter] == 'O' || input[counter] == '6')
{
endInput = endInput.Insert(counter, "6");
}
else if (input[counter] == 'P' || input[counter] == 'Q' || input[counter] == 'R' || input[counter] == 'S' || input[counter] == '7')
{
endInput = endInput.Insert(counter, "7");
}
else if (input[counter] == 'T' || input[counter] == 'U' || input[counter] == 'V' || input[counter] == '8')
{
endInput = endInput.Insert(counter, "8");
}
else if (input[counter] == 'W' || input[counter] == 'X' || input[counter] == 'Y' || input[counter] == 'Z' || input[counter] == '9')
{
endInput = endInput.Insert(counter, "9");
}
}
TelephoneFormat(ref endInput);
label3.Text = endInput;
}
else
{
MessageBox.Show("Invalid Number");
}
}
}
}
Explanation / Answer
Try this for your reference,
You also shouldn't be doing work in event handlers. They should call methods that do the work. Better design. You also have a lot of redundant code, for example your IsValidNumber method (which doesn't do what it says it does, all it does is check the length. I could type 10 * in there and it would be happy. IsValidInputLength would be a better name .You also set values that have already been set. I'd rework it like this:
private readonly Dictionary<int, char[]> keys =Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.