Cant figure out why it giving me error at line 116, can anyone please tell me where did i go wrong?
Thanks!
Some people provide phone numbers using one or more alphabetic characters. Write a method that will take a single letter and display the corresponding number. You may want to take a look at your telephone for this...an old telephone that is, your smartphone will not work for this. If the character does not correspond to any number display a message stating the fact. If the character is a numerical digit, the same numerical digit must be returned. Allow upper case and lowercase characters to be accepted. In the main, write a small application that will ask the user for a "fancy" phone number, and by calling the method, display the equivalent in just numerical digits.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Shaker
{
class Program
{
public static void Main()
{
Console.Write("Enter Phone Number: ");
convertcharacter1();
}
private static void convertcharacter1()
{
int i = 0;
string character1 = Console.ReadLine();
Console.WriteLine(character1);
while (true)
{
char character = char.Parse(character1.Substring(i, 1).ToLower());
switch (character)
{
case '0':
character1 += "0";
break;
case '1': character1 += "1";
break;
case '2': character1 += "2";
break;
case '3': character1 += "3";
break;
case '4': character1 += "4";
break;
case '5': character1 += "5";
break;
case '6': character1 += "6";
break;
case '7': character1 += "7";
break;
case '8': character1 += "8";
break;
case '9': character1 += "9";
break;
case '-': character1 += "-";
break;
case 'a':
case 'b':
case 'c': character1 += "2";
break;
case 'd':
case 'e':
case 'f': character1 += "3";
break;
case 'g':
case 'h':
case 'i': character1 += "4";
break;
case 'j':
case 'k':
case 'l': character1 += "5";
break;
case 'm':
case 'n':
case 'o': character1 += "6";
break;
case 'p':
case 'q':
case 'r':
case 's': character1 += "7";
break;
case 't':
case 'u':
case 'v': character1 += "8";
break;
case 'w':
case 'x':
case 'y':
case 'z': character1 += "9";
break;
i++;
}
}
}
}
Loading
theLizardPosted Feb 25, 2012, 5:27 PM
Most people do not see past the text book solutions, in your case you are trying to match characters, what you should be doing is matching charachters by their value ie 0 = 48, a = 97, A = 67
Try this code, I have only done to 0, 1, 2 (abc) and 3 (def) for you, you do the rest..
//----------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
string s = "abc123456";
string b;
char c;
for (int i = 0; i < s.Length; i++)
{
b = GetCharValue((c = Convert.ToChar(s.Substring(i, 1))));
}
}
//----------------------------------------------------------------------
private string GetCharValue(char c)
{
string s="";
if (c == 48) //0
s = Convert.ToString(c);
if(c == 49) //1
s = Convert.ToString(c);
if (c == 50 || c >= 65 && c <= 67 || c >= 97 && c <= 99) //2
s = "2";
if (c == 51 || c >= 68 && c <= 70 || c >= 100 && c <= 102) //3
s = "3";
return (s);
}
Sam HobbsPosted Feb 26, 2012, 7:14 AM
Note that I answered your original question. You should have created a new thread for the new question.
Shaker AlSalemPosted Feb 26, 2012, 2:19 AM
Thanks.
Sam HobbsPosted Feb 25, 2012, 11:53 PM
It would have taken more of my time for me to teach Shaker how to think like a programmer so Shaker would be better prepared in the future. It is not a matter of me being lazy or disrespectful. It is more a matter of having respect for a person's ability to learn instead of doing the work for them.
theLizardPosted Feb 25, 2012, 8:13 PM
One problem with text book solutions is that it inhibits people from thinking about real world solutions, by this I mean using text book examples without understanding how and why the code works.
Take List
If the OP takes my code and uses it without trying to understand the principles that make it work then the OP will NOT become a programmer, just a code hacker.
If my code does not help the OP see that there are alternatives to every solution then what can I say.
The only way that people will learn is by understanding what goes on, if you don't know what goes on you can never develop new solutions to simple tasks when you are confronted with new problems relating to the same task.
I do not use C# for anything other than web development in code behind, It took me about 2 minutes to provide an alternative to what the OP was doing which to me looked like a painful way for programming a simple task.
The op was doing things the hard way, I am showing an easier way with less code and without using too much of the built in functionality that clouds creative solutions, the OP can learn by this or not, that is the OPs decision.
I also think that by showing real world examples, people will learn much faster.
Prime bPosted Feb 25, 2012, 7:31 PM
Sam HobbsPosted Feb 25, 2012, 7:17 PM
Sam HobbsPosted Feb 25, 2012, 7:13 PM
you can just use:
Prime bPosted Feb 25, 2012, 6:50 PM
Sam HobbsPosted Feb 25, 2012, 6:16 PM
You are certainly correct that the problem can be done much easier. I was trying to teach the student how to program. Sample code helps but it does not teach how to think in terms of what to do and then converting that to code.
Sam HobbsPosted Feb 25, 2012, 4:35 PM
I think the foreach is easy to understand. You want to do it for each character in the string.
Shaker AlSalemPosted Feb 25, 2012, 3:24 PM
Thanks a lot!
Sam HobbsPosted Feb 24, 2012, 7:22 PM
So I think instead of using character1 as the output you need to put the new characters into a different string.
Instead of using a "while" statement you can use a "for" or "foreach" statement. If you use a for statement then you can start at 0 and end at the string length and increment by 1. So then you can remove the line that has "++i;". I could tell you exactly what the source code should be but it will be better it you figure it out. If you use a foreach then you can do your loop for each character in the string.
Then at the end of the program put a statement to show the result.
Shaker AlSalemPosted Feb 24, 2012, 6:17 PM
Thanks.
Sam HobbsPosted Feb 23, 2012, 2:50 AM
My guess is that you have an unbalanced brackets condition. I think you are missing an end bracket ("}"). In other words, I think you have one more "{" than "}".