Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Telephone Number

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Telephone Number

    Can't get the correct program.

    To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, the telephone number 438-5626 can be shown as GET-LOAN. In some cases, to make a telephone number
    meaningful, companies might use more than seven letters. For example,225-5466 can be displayed as CALL-HOME, which uses eight letters. Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more than eight letters, then process only the first seven letters. Also, output the – (hyphen) after the third digit. Allow the user to use uppercase
    and lowercase letters, as well as spaces between words. Moreover, your program should process as many telephone numbers as the user wants.

    (Hint: You can read the entered telephone number as a string and then use the charAt method of the class String to extract each character.
    For example, if str refers to a string, then the expression str.charAt(i) returns the character at the ith position.
    Recall that in a string, the position of the first character is 0.)

    Always ask the user if another set of values will be entered.
    ----------------------------------------------------------------------------------------------------------
    Program:

    import java.util.Scanner;

    public class Exam_TelephoneNumber{
    public static void main(String [] args) throws Exception
    {
    Scanner console = new Scanner (System.in);

    String answer;
    char intAns;

    System.out.println("============================== =================================================" );
    System.out.println("About: Outputs the equivalent telephone number in letters to its corresponding\ntelephone digit.");
    System.out.println("============================== =================================================" );
    System.out.println("");
    System.out.println("\t\t\t\t* TELEPHONE DIGITS *");
    System.out.println("");

    do{
    System.out.print("Enter the telephone number (in letters): ");
    String telNum = console.nextLine();
    char let1 = telNum.charAt(0);
    char let2 = telNum.charAt(1);
    char let3 = telNum.charAt(2);
    char let4 = telNum.charAt(3);
    char let5 = telNum.charAt(4);
    char let6 = telNum.charAt(5);
    char let7 = telNum.charAt(6);
    char let8 = telNum.charAt(7);

    String num1 = "", num2 = "", num3 = "", num4 = "", num5 = "", num6 = "", num7 = "", num8 = "";
    switch (let1)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num1 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num1 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num1 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num1 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num1 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num1 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num1 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num1 = "9"; break;
    }
    switch (let2)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num2 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num2 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num2 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num2 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num2 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num2 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num2 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num2 = "9"; break;
    }
    switch (let3)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num3 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num3 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num3 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num3 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num3 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num3 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num3 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num3 = "9"; break;
    }
    switch (let4)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num4 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num4 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num4 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num4 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num4 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num4 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num4 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num4 = "9"; break;
    }
    switch (let5)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num5 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num5 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num5 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num5 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num5 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num5 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num5 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num5 = "9"; break;
    }
    switch (let6)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num6 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num6 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num6 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num6 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num6 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num6 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num6 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num6 = "9"; break;
    }
    switch (let7)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num7 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num7 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num7 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num7 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num7 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num7 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num7 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num7 = "9"; break;
    }
    switch (let8)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num8 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num8 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num8 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num8 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num8 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num8 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num8 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num8 = "9"; break;
    }
    System.out.println("");
    System.out.println("The number is: " + num1 + num2 + num3 + "-" + num4 + num5 + num6 + num7 + num8);
    System.out.println("");
    System.out.print("Do you want to input another integer (Y - yes / No - any key): ");
    answer = console.next();
    intAns = answer.charAt(0);
    System.out.println("");

    }while (intAns == 'Y' || intAns == 'y');

    System.out.println("============================== =================================================" );
    System.out.println("\t\t\t\t* END OF PROGRAM *");
    System.out.println("============================== =================================================" );
    }
    }

    ----------------------------------------------------------------------------------------------------------
    Output:

    * TELEPHONE DIGITS *

    Enter the telephone number (in letters): GOTO hell

    The number is: 468-6435

    Do you want to input another integer (Y - yes / No - any key): y

    Enter the telephone number (in letters): GO TOHELL

    The number is: 46-86435 <--- this should be 468-6435

    Do you want to input another integer (Y - yes / No - any key): n

    ================================================== =============================
    * END OF PROGRAM *
    ================================================== =============================

    Process completed.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,290
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Telephone Number

    Please post your code in code tags. Learn how here.

    Also, let us know what you need help with. Ask questions, describe any problems you're having, post error messages that you don't know how to fix. Don't make us guess what help you want. We're usually wrong.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Telephone Number

    Enter the telephone number (in letters): GO TOHELL

    The number is: 46-86435 <--- instead of 46-86435, it should be 468-6435

    --- Update ---

    import java.util.Scanner;
     
    public class Exam_TelephoneNumber{
    public static void main(String [] args) throws Exception
    {
    Scanner console = new Scanner (System.in);
     
    String answer;
    char intAns;
     
    System.out.println("============================== =================================================" );
    System.out.println("About: Outputs the equivalent telephone number in letters to its corresponding\ntelephone digit.");
    System.out.println("============================== =================================================" );
    System.out.println("");
    System.out.println("\t\t\t\t* TELEPHONE DIGITS *");
    System.out.println("");
     
    do{
    System.out.print("Enter the telephone number (in letters): ");
    String telNum = console.nextLine();
    char let1 = telNum.charAt(0);
    char let2 = telNum.charAt(1);
    char let3 = telNum.charAt(2);
    char let4 = telNum.charAt(3);
    char let5 = telNum.charAt(4);
    char let6 = telNum.charAt(5);
    char let7 = telNum.charAt(6);
    char let8 = telNum.charAt(7);
     
    String num1 = "", num2 = "", num3 = "", num4 = "", num5 = "", num6 = "", num7 = "", num8 = "";
    switch (let1)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num1 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num1 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num1 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num1 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num1 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num1 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num1 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num1 = "9"; break;
    }
    switch (let2)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num2 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num2 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num2 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num2 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num2 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num2 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num2 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num2 = "9"; break;
    }
    switch (let3)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num3 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num3 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num3 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num3 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num3 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num3 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num3 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num3 = "9"; break;
    }
    switch (let4)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num4 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num4 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num4 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num4 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num4 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num4 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num4 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num4 = "9"; break;
    }
    switch (let5)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num5 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num5 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num5 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num5 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num5 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num5 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num5 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num5 = "9"; break;
    }
    switch (let6)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num6 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num6 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num6 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num6 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num6 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num6 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num6 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num6 = "9"; break;
    }
    switch (let7)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num7 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num7 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num7 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num7 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num7 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num7 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num7 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num7 = "9"; break;
    }
    switch (let8)
    {
    case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': num8 = "2"; break;
    case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': num8 = "3"; break;
    case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': num8 = "4"; break;
    case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': num8 = "5"; break;
    case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': num8 = "6"; break;
    case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': num8 = "7"; break;
    case 'T': case 't': case 'U': case 'u': case 'V': case 'v': num8 = "8"; break;
    case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': num8 = "9"; break;
    }
    System.out.println("");
    System.out.println("The number is: " + num1 + num2 + num3 + "-" + num4 + num5 + num6 + num7 + num8);
    System.out.println("");
    System.out.print("Do you want to input another integer (Y - yes / No - any key): "); 
    answer = console.next();
    intAns = answer.charAt(0);
    System.out.println("");
     
    }while (intAns == 'Y' || intAns == 'y');
     
    System.out.println("============================== =================================================" );
    System.out.println("\t\t\t\t* END OF PROGRAM *");
    System.out.println("============================== =================================================" );
    }
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,290
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Telephone Number

    Wow! If you'd set out to code the hardest way possible to do this assignment, you would have come up with what you've posted.

    Have you not learned arrays, loops, or taken a look at the String API?

    It appears you need to program how to handle a space in the user's input.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Telephone Number

    It is possible he is a new student or that has just started learning as some colleges have just started up again.

    As for the OP you must understand how Strings work and what the charAt does. For example what happens if the user enters

    C A L L HOME
    what should come out: 225-5466 What will come out?

    How do you handle white space?

    For now your program will be limited. It expects the user to enter a string in the correct format (ABC DEFG). If you want to simplify your code a little more take a look and understand nested loops. Why they are helpful and what function they can serve. Array's as mentioned by GregBrannon are also a way to make everything cleaner. They are a little difficult to comprehend at first but they are incredible once you learn how easy they are to actually use.

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Telephone Number

    I'm sorry. I'm just a newbie. And she just taught us how to use, If else statements, Switch and Looping. I don't know what array, or String API is.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Telephone Number

    Quote Originally Posted by Haiz View Post
    use, If else statements, Switch and Looping.
    In that case loop through the input and get one digit at a time from a method that has uses a switch.

  8. #8
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Telephone Number

    Hello.
    HINT: Before processing the input text you may chop off the spaces available in the input.

    Syed.

Similar Threads

  1. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  2. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  3. HELP!!! Entry Class to represent entries in a telephone directory
    By Princess D in forum Java Theory & Questions
    Replies: 10
    Last Post: January 22nd, 2010, 05:39 AM
  4. How to detect audio input from a telephone call?
    By ces_31 in forum Java Theory & Questions
    Replies: 0
    Last Post: August 12th, 2009, 01:45 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 02:04 AM