Java Beginners

Moderators: zibadian
Number of threads: 1285
Number of posts: 2739

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Translating letters to numbers using charAt help Posted by wrnickens on 8 Oct 2011 at 9:31 AM
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");
}

}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.