phone number converter

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2277 Views - Last Post: 14 February 2012 - 04:34 PM Rate Topic: -----

#1 AKadado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-February 12

phone number converter

Posted 12 February 2012 - 07:25 PM

hi,
I want to create a program that converts vanity phone numbers
for example : convert 1-800-FLOWERS to 1-800-356-9377
I know that I have to use a switch to convert the letters into numbers but I can't figure out a way to verify each letter and convert it

this is the code that I wrote so far.
any pointers would be appreciated .
private static void convertPhoneNumber()
        {
            string phonenumber = "";
            for (int i = 0; i < 11; i++)
            {
                
                {
                    
                    switch (character)
                    {
                        case '0': phonenumber += "0";
                            break;

                        case '1': phonenumber += "1";
                            break;

                        case '2': phonenumber += "2";
                            break;

                        case '3': phonenumber += "3";
                            break;

                        case '4': phonenumber += "4";
                            break;

                        case '5': phonenumber += "5";
                            break;

                        case '6': phonenumber += "6";
                            break;

                        case '7': phonenumber += "7";
                            break;

                        case '8': phonenumber += "8";
                            break;

                        case '9': phonenumber += "9";
                            break;

                        case '-': phonenumber += "-";
                            break;

                        case 'a':
                        case 'b':
                        case 'c': phonenumber += "2";
                            break;

                        case 'd':
                        case 'e':
                        case 'f': phonenumber += "3";
                            break;

                        case 'g':
                        case 'h':
                        case 'i': phonenumber += "4";
                            break;

                        case 'j':
                        case 'k':
                        case 'l': phonenumber += "5";
                            break;

                        case 'm':
                        case 'n':
                        case 'o': phonenumber += "6";
                            break;

                        case 'p':
                        case 'q':
                        case 'r':
                        case 's': phonenumber += "7";
                            break;

                        case 't':
                        case 'u':
                        case 'v': phonenumber += "8";
                            break;

                        case 'w':
                        case 'x':
                        case 'y':
                        case 'z': phonenumber += "9";
                            break;

                            return phoneNumber;
                    }

                }
            }
        }


Is This A Good Question/Topic? 0
  • +

Replies To: phone number converter

#2 cilaes   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 75
  • Joined: 12-December 11

Re: phone number converter

Posted 12 February 2012 - 09:36 PM

You should get an error when running that code. Take a look at line 7, what's that curly bracket there for?

Also, what exactly do you mean exactly by verify each letter?
Was This Post Helpful? 0
  • +
  • -

#3 AKadado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-February 12

Re: phone number converter

Posted 12 February 2012 - 09:47 PM

let's say that I type 1800flowers, I want the program to read the "f" , look up its number and return it.
Or does the switch function do this job ?

I'll fix the extra curly bracket
Was This Post Helpful? 0
  • +
  • -

#4 Mystic   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 28-August 07

Re: phone number converter

Posted 13 February 2012 - 02:16 AM

You could use a combination of a for loop and the switch statement (or if statements). Are the dashes ("-") required for the number, also are you allowed a combination of numbers and letters such as 1-800-ROSES4U? Considering you will end up with a number, and it seems the lettering only starts after the fourth digit (I assume thats an area code, after the country code), you can just check the substring from the 5th position (assuming no "-").
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: phone number converter

Posted 13 February 2012 - 06:44 AM

Reminder to new folks: This is homework and we don't provide completed code for homework. Rather we try to help the student comprehend the concept they are struggling with so they can write their own code. They are being graded on their skill not ours.

This is the 'Help me with my homework' article you wanted.
Was This Post Helpful? 0
  • +
  • -

#6 AKadado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-February 12

Re: phone number converter

Posted 13 February 2012 - 11:16 AM

View PostMystic, on 13 February 2012 - 02:16 AM, said:

You could use a combination of a for loop and the switch statement (or if statements). Are the dashes ("-") required for the number, also are you allowed a combination of numbers and letters such as 1-800-ROSES4U? Considering you will end up with a number, and it seems the lettering only starts after the fourth digit (I assume thats an area code, after the country code), you can just check the substring from the 5th position (assuming no "-").


Thanks, that's what I did (for loop and a switch)
No need for the dashes and yes you can enter a combination.
But how can I use the substring ?
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: phone number converter

Posted 13 February 2012 - 11:21 AM

Quote

But how can I use the substring ?


How are you obtaining "character"?

09 switch (character)

One way would be to get the substring of the total phone number..


substring(0,1) for the first digit
substring(1,1) for the 2nd digit
Of course you would use your loop index for the starting position.
Was This Post Helpful? 0
  • +
  • -

#8 AKadado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-February 12

Re: phone number converter

Posted 13 February 2012 - 07:02 PM

View PosttlhIn`toq, on 13 February 2012 - 11:21 AM, said:

Quote

But how can I use the substring ?


How are you obtaining "character"?

09 switch (character)

One way would be to get the substring of the total phone number..


substring(0,1) for the first digit
substring(1,1) for the 2nd digit
Of course you would use your loop index for the starting position.


oh my bad I didn't explain what I mean by "character".
what I mean is the character being evaluated at the moment(I'm sorry I'm having a hard time explaining myself)
like when it arrives to the letter f check what integer corresponds to it and return it, but since I didn't know what to put between the brackets I just put character.

I'm sorry I just cant figure how to use the substring in the code
Was This Post Helpful? 0
  • +
  • -

#9 superkb10   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: phone number converter

Posted 13 February 2012 - 08:27 PM

Here's a completely new idea, but try just making whatever comes in, assign a string that value. So if you're doing a Console Application do a ReadLine and if you're doing a Windows Forms application do a string = TextBox.Text;

Then just use the Replace function on that string, replacing the characters with their respective values in numbers. Then just put out that string.
Was This Post Helpful? 0
  • +
  • -

#10 AKadado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-February 12

Re: phone number converter

Posted 13 February 2012 - 09:36 PM

View Postsuperkb10, on 13 February 2012 - 08:27 PM, said:

Here's a completely new idea, but try just making whatever comes in, assign a string that value. So if you're doing a Console Application do a ReadLine and if you're doing a Windows Forms application do a string = TextBox.Text;

Then just use the Replace function on that string, replacing the characters with their respective values in numbers. Then just put out that string.



Is this what you mean ??
private static void convertPhoneNumber()
        {
            string phoneNumber;
            phoneNumber = Console.ReadLine();

            string newPhoneNumber = phoneNumber.Replace("a", "2");
            Console.WriteLine(newPhoneNumber);

        }

I tried to compile it but I got the error "Cannot implicitly convert type 'string' to 'int'
Was This Post Helpful? 0
  • +
  • -

#11 Mystic   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 28-August 07

Re: phone number converter

Posted 14 February 2012 - 05:48 AM

You could use
int.parse(string)
to solve your error. Note I haven't used the .replace before so Im not 100% sure what it does, I assume it searches the string and replaces all instances of a given character with whatever you want.


On the substring, within your for loop, you check each individual character and then match it against your switch statement. Output the result to a temp string.

To help you with the logic, the for loop only loops the length of the word in the phone number. In your substring you only specify a length of 1, your starting position obviously at the desired letter, remember you are looping so you can't hardcode the position, say 4.

Edit: Thanks tlhIn`toq for the +1

This post has been edited by Mystic: 14 February 2012 - 05:51 AM

Was This Post Helpful? 1
  • +
  • -

#12 AKadado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-February 12

Re: phone number converter

Posted 14 February 2012 - 12:42 PM

So I ended up using the switch and this is what I have so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


/*
 ************************************************************************
 ***     Programming Assignment #2                                    ***
 ***     Developer: -------------                                     ***
 ***     Date Submitted: February 14 2012                             ***
 ***     Purpose: 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    ***
 ************************************************************************
 */

namespace question_2
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter the phone number: ");
            convertPhoneNumber();
        }

        private static void convertPhoneNumber()
        {
            string phonenumber = Console.ReadLine();
            int i = 0;
            for (i = 0; i < 10; i++)
            {
                string Newphonenumber = phonenumber.Substring(i, 1);

                char character = Convert.ToChar(Newphonenumber);
                               
                    switch (character)
                    {
                        case '0': phonenumber += "0";
                            break;

                        case '1': phonenumber += "1";
                            break;

                        case '2': phonenumber += "2";
                            break;

                        case '3': phonenumber += "3";
                            break;

                        case '4': phonenumber += "4";
                            break;

                        case '5': phonenumber += "5";
                            break;

                        case '6': phonenumber += "6";
                            break;

                        case '7': phonenumber += "7";
                            break;

                        case '8': phonenumber += "8";
                            break;

                        case '9': phonenumber += "9";
                            break;

                        case '-': phonenumber += "-";
                            break;

                        case 'a':
                        case 'b':
                        case 'c': phonenumber += "2";
                            break;

                        case 'd':
                        case 'e':
                        case 'f': phonenumber += "3";
                            break;

                        case 'g':
                        case 'h':
                        case 'i': phonenumber += "4";
                            break;

                        case 'j':
                        case 'k':
                        case 'l': phonenumber += "5";
                            break;

                        case 'm':
                        case 'n':
                        case 'o': phonenumber += "6";
                            break;

                        case 'p':
                        case 'q':
                        case 'r':
                        case 's': phonenumber += "7";
                            break;

                        case 't':
                        case 'u':
                        case 'v': phonenumber += "8";
                            break;

                        case 'w':
                        case 'x':
                        case 'y':
                        case 'z': phonenumber += "9";
                            break;

                           
                    }
                    

                }
                  Console.WriteLine(phonenumber);
            }
        }
    }



but the output is printing the input and the result
how can I make it only print the result ?

This post has been edited by Curtis Rutland: 14 February 2012 - 03:04 PM
Reason for edit:: redacted personal information

Was This Post Helpful? 0
  • +
  • -

#13 AKadado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-February 12

Re: phone number converter

Posted 14 February 2012 - 01:00 PM

I know my name is showing and assignment... but I couldn't find the edit button.. so be it :P
Was This Post Helpful? 0
  • +
  • -

#14 Mystic   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 28-August 07

Re: phone number converter

Posted 14 February 2012 - 02:00 PM

You dont need line 35, just change "Convert.ToChar(Newphonenumber);" to "Convert.ToChar(phonenumber.Substring(i, 1));" and you also need a "ToLower" in case you get a phone number such as "1-800-FuNkYNumBeR".
As for the output, you are printing the input twice because in your case statements you said
case '0': phonenumber += "0";
                            break;



Which means "take what is in phonenumber and add 0 (or any of the other corresponding numbers)". You should declare an empty string (String temp = "") and change your case statements to

case '0': temp += "0";
                            break;


You could also do your code like this

static void Main(string[] args)
        {
            String sNumber;
            Console.WriteLine("Enter the phone number: ");
            sNumber = Console.ReadLine();   
            sNumber = Convert(sNumber);
            Console.WriteLine(sNumber);
        }

Private static String Convert (String _Number)
{
            String sTemp = "";
            char C;
            //loop
            {
                C = char.Parse(_Number.Substring(i, 1).ToLower());   //   the ToLower is needed as you arent checking both upper case and lowercase in your case statements ('A' and 'a')
                //switch
                {
                   sTemp += new numeral from character
                }
            }
            return sTemp;
}




Depending on your requirements, you dont have to check the first X characters as they are already numerals.
Also your loop shouldnt have a fixed loop of 10, what happens if the number is longer than 10 or shorter (it shouldnt crash on less than 10 but it just wastes time doing the same thing to the same last character)? Rather put the count limit to the length of the string.

This post has been edited by Mystic: 14 February 2012 - 02:13 PM

Was This Post Helpful? 1
  • +
  • -

#15 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: phone number converter

Posted 14 February 2012 - 03:05 PM

I've removed your name from your posted code. New members have restrictive edit times, since many try to post their code, get an answer, then delete their post text. So, in the future, just be careful, and if you make a mistake, you can report the post. We'll remove personally identifying information, but for anything other than that, we'll need a very convincing reason to edit a post.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2