Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Translating letters to numbers using charAt help

So far this semester I've had zero problems learning java but looping doesn't seem to make sense to me. I have an assignment in which i have to translate vanity phone numbers (phone numbers that contain letters) into numbers. The program also has to output a hyphen after the third digit, allow the user to use uppercase or lowercase, as well as spaces between words, and should only process the first 7 digits and ignore anymore. The program needs to be able to process as many numbers as the user wants and should have a stop feature.

Here's the hint the book gives, "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 eh ith position. Recall that in a string, the position of the first character is 0."

Here is what i have so far....any help or pointers would be great!


import java.util.*;

public class page296Prob5 {

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();

for (int i=0;i<str.length();i++)
System.out.print(letter(str.charAt(i)));
System.out.println();

static char letter(char c) {
switch (c)
{

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

}
Sign In or Register to comment.