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");
}
}