Hello,

I have to write a program that prompts the user to enter a phone number expressed in letters and outputs the corresponding phone numbers in digits. The user can use lowercase and uppercase letters, as well as spaces between words. And I am supposed to use the charAt method to extract each character. I figured I can use each charAt position in a switch and check each letter from there, but the coding get's to long. But I wanted to ask if there is a better way to code this, using a loop maybe to increment each character and just one switch statement. This is what I have so far.

Thank you.

import java.util.*;
public class phoneDigits {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {

        String str;
        char letter;




        System.out.println("Enter phone number expressed in letters: ");
        str = console.next();
        System.out.println();




        if ((letter >= 'A' || letter >= 'a') && (letter <='Z' || letter <= 'z'))
        {

            switch (str.charAt(0))
            {
            case 'A': case 'a': case 'B': case 'b':
            case 'C': case 'c': System.out.print("2");
            break;
            case 'D': case 'd': case 'E': case 'e':
            case 'F': case 'f': System.out.print("3");
            break;
            case 'G': case 'g': case 'H': case 'h':
            case 'I': case 'i': System.out.print("4");
            break;
            case 'J': case 'j': case 'K': case 'k':
            case 'L': case 'l': System.out.print("5");
            break;
            case 'M': case 'm': case 'N': case 'n':
            case 'O': case 'o': System.out.print("6");
            break;
            case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r':
            case 'S': case 's': System.out.print("7");
            break;
            case 'T': case 't': case 'U': case 'u':
            case 'V': case 'v': System.out.print("8");
            break;
            case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y':
            case 'Z': case 'z': System.out.print("9");
            break;
            default: System.out.print(" ");

            }
            switch (str.charAt(1))
            {
            case 'A': case 'a': case 'B': case 'b':
            case 'C': case 'c': System.out.print("2");
            break;
            case 'D': case 'd': case 'E': case 'e':
            case 'F': case 'f': System.out.print("3");
            break;
            case 'G': case 'g': case 'H': case 'h':
            case 'I': case 'i': System.out.print("4");
            break;
            case 'J': case 'j': case 'K': case 'k':
            case 'L': case 'l': System.out.print("5");
            break;
            case 'M': case 'm': case 'N': case 'n':
            case 'O': case 'o': System.out.print("6");
            break;
            case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r':
            case 'S': case 's': System.out.print("7");
            break;
            case 'T': case 't': case 'U': case 'u':
            case 'V': case 'v': System.out.print("8");
            break;
            case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y':
            case 'Z': case 'z': System.out.print("9");
            break;
            default: System.out.print(" ");
            }
            switch (str.charAt(2))
            {
            case 'A': case 'a': case 'B': case 'b':
            case 'C': case 'c': System.out.print("2");
            break;
            case 'D': case 'd': case 'E': case 'e':
            case 'F': case 'f': System.out.print("3");
            break;
            case 'G': case 'g': case 'H': case 'h':
            case 'I': case 'i': System.out.print("4");
            break;
            case 'J': case 'j': case 'K': case 'k':
            case 'L': case 'l': System.out.print("5");
            break;
            case 'M': case 'm': case 'N': case 'n':
            case 'O': case 'o': System.out.print("6");
            break;
            case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r':
            case 'S': case 's': System.out.print("7");
            break;
            case 'T': case 't': case 'U': case 'u':
            case 'V': case 'v': System.out.print("8");
            break;
            case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y':
            case 'Z': case 'z': System.out.print("9");
            break;
            default: System.out.print(" ");

            }

        }
        else
            System.out.println("Invalid input");



    }

}

(1) write a static method where the essential code has been already made as you posted:

static char letterToInt(char c) {
     switch (c)
			{
			case 'A': case 'a': case 'B': case 'b':
			case 'C': case 'c': return '2';  
/* no "break" is needed becasue we have had the "return" */
			case 'D': case 'd': case 'E': case 'e':
			case 'F': case 'f': return '3';
...
return '0'

(2) the main() method will call the static char letterToInt(char c) to conver each English letter into digital character:

...
	System.out.println("Enter phone number expressed in letters: ");		
         str = console.next();

	for (int i=0;i<str.length();i++)
	System.out.print(letterToInt(str.charAt(i)));  // to convert each English letter into digital character and print it
	System.out.println();

(3) Why the English letters can not refer to the digital characters '0' and '1'?

commented: nice - Xufyan +0

Thank you very much. I just started learning JAVA so I can't use the static method with the return statement yet, but with the for loop you gave me I got it to work. Thanks again !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.