ALPHABETIC TELEPHONE NUMBER TRANSLATOR for Class
I have been trying to figure this out for a few days now. Here is the full question:
"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 am very new to c# and understand basic code. I am able to code it to where it accepts the inputted Alphabetic phone number converted it to a char array. I am sure I have to use some type of foreach loop or something like that. I have a good idea on what I am supposed to do, but I cannot put it into code. I have tried looking up the question and found a few solutions, but I do not understand all of it (which is important to me). Also, I will be launching it with the click of a button
private void button1_Click(object sender, EventArgs e)
84 Replies
What have you tried so far?
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/
So I tried a version of this but I have already deleted it and tried to start over
I was looking online and that is what I found, but I cannot figure what all this means, how to make it work for a button etc.
I have the Enum, I have the char array from the input of the user
My idea was using the foreach, it determines whether the var is a letter or number. If a number, it assigns it to the ConvNumbersArr. If a letter, it uses the enum to convert it to its enum counterpart, then it assigns it to ConvNumbersArr. Then print after its finished running through all of the AlphNumbersArr.
Hope that makes sense.
your plan sounds right
My main issue is the meat of the code. I cannot put my plan into code haha
well you already have your loop startred,
character contains the letter or number that gets currently "checked"
now like you wrote check if its letter or digit or dash
I would do an if else right? How can I make that
or something like that?
yea you can work with that
so if its a letter you wanted to call your convert method
Yes!
go on
character.isLetter gives an error, how would I correctly write that?
try
Char.IsLetter(character)
Okay that passed. Now here is my other problem and I apologize. I need to create the convert method.
Is it able to access a convert method that I have to make within the if statement?
yes, you made it your self easy in this regard since the class and method is static you can call the method by writing PhoneNumber.ParseInput(character)
are you referencing this?
yea
I got that method online and I have no idea how it works. there is a lot that goes into that to which I dont understand lol
so you want to rewrite that to something you understand?
Yes exactly
do you have an idea how to tackle this?
im trying to not spoonfeed you since this homework and you should learn and not copypaste. but i bring you on the right track if you get stuck somewhere ;)
I do appreciate that, i would rather have it that way
do you think enum would be okay here?
since you have already the groundwork set to start checking each individual character how would you formulate the steps needed to convert the letter into the right number
I would have it reference the enum somehow and find its counterpart in the enum
thats not what i mean, forget the enum for a moment..
in plain simple english words what needs to be done?
I probably know but I am completely overlooking it
to be honest you already wrote like 90% of it in initial post
Okay, are you saying the one with the PhoneNumber method?
I just dont understand it:/ i apologize
ok i guess im too vague
No I am just a huge noob loo
lol
soooo
you are looping over every char right?
so now you need to convert that char into a number
if its 'A' or 'B' or 'C'
that is a 2
if its 'D' or 'E' or 'F'
that is a 3
....
if you spell it out like this you can already kind of see the program that has to be written ;)
Yes! I tried writing something like that, but I could not format it correctly
I tried to use a switch
well stick to the basics first ;) show me how you have written it with ifs
something like this? I cannot format that correctly though. I know I am on the right track though
to explain why it hase to be written like this
after every || starts a new condition that has to be met
just writing 'B' is not a condition that can be evaluated
would it be char instead?
you are missing ==
twice
AHHHHH
thank you
so much
so to continue
So the value would be assigned to ConvNumbersArr right?
yes
Should I use a char array or a regular array for ConvNumbers
waht is a regular array for you?
Just a single dimensional array
integer array
Sorry lol there isnt a regular array
I mean an integer array
Wait I dont even need one
ok we have 2 problems/misunderstandings here
1. you dont need an extra array to fill an array(good job figuering it out yourself)
2. since you are using an array you have to tell it how long that array should be and when adding to that array you have to specify where in that array you want to fill the content
char[] ConvNumbersArr = new char[10];
that works for the converted phone number yes? 10 digit long
this is part of a solution yes
But, you are saying I dont even need that array
I can just reassign the values in that array?
in AlphNumbers
yes you can do this
but this isnt really what i wanted to point to
lets say you want to say the 3rd field in a array, doesnt matter which, should be 'B', how do you do this?
this is still missing in your code
ConvNumbersArr[2] = 'B';
yes but in your code where does the 2 come from?
The 2 is the third field correct?
yes
0 = first
var AlphNumbersArr = AlphNumber.ToCharArray(0,9);
I declared 10 maximum fields for this right?
no you misunderstood me
so when you convert the first letter or number it goes into ConvNumbersArr[0]
the second goes into ConvNumbersArr[1] and so on.
Yes, I was thinking about that right now. How would I get the foreach to know to paste it in the next value of the array?
because If i do something like this, it will just paste it into the first one
you use a number that you increment every time you run through the loop
Where would I put that at?
wanna take a guess?
I would say in the if?
the if following the foreach
what exactly do you want to put there?
I would have to put something like i++
Would I have to use a for ?
I think i understand I just dont know how to code it
ok yea that is a possibility, not really clean but possible, but there is still the open question where you create the i
i would be the index of the array
a for loop would also work and here most likely be the most correct choice but you arent forced to use it
So i can use a for after a foreach?
try to write the code how you think it could work, we can fix whats wrong together then
Hows that look
when i wrote that you could use a for loop i meant instead of foreach
remove the for again and then just write int i = 0; above foreach
oh haha sorry
and then at the end write i++;
but still inside foreach
like this?
int i = 0;
would i need to change character to i
like this
Okay thanks
I did not know i++ could be there at the bottom like that
sadly i dont have much time left so let me quickly type a few comments what to do and where to do maybe somene else can take over
`
Thank you very much for your time
You have been greatly helpful 🙂
I think I got the worst of it down, thanks to you
@ACiDCA7 are you available for a question really quick?
really quick
Would this be correct to determine if it is a digit and throw it into the array?
seems reasonable
Okay, thanks
I am just really trying to understand the code, i apologize
asuming you dont have changed too much you could ignore additional checks if its digits/dashes since only letters should be converted and nothing else
Okay cool, thank you
so if letter convert else just add to your array
The dashes would cause some problems because it surpasses the character limit of 10 we set on the array right?