• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with String methods & forloops  RSS feed

 
Shaylen Patel
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Introduction
The international standard letter/number mapping found on the telephone is as follows:
1
2
ABC
3
DEF
4
GHI
5
JKL
6
MNO
7
PQRS
8
TUV
9
WXYZ
0

Write a method that returns a number, given an uppercase letter using the method definition:
public static int getNumber(char upperCaseLetter)

Input
phoneNumberStr - String

Output
phoneNumber translated to numbers - String

Sample Output:
Enter the phone number string
1-800-flowers
Phone: 1-800-3569377

Enter the phone number string
1800FLOWERS
Phone: 18003569377

Directions
1. Before each significant step, provide a comment explaining the step (do not comment
every line of code).

2. The input is a String. It is recommended you immediately convert this String to
uppercase since 1-800-flowers and 1-800-FLOWERS would both display 1-800-
3569377 as the phone number.

3. You MUST use a for-loop to step through phoneNumberStr.

4.Within the for-loop body, convert only the letters to their equivalent number using the
following algorithm:
a. If the current character of phoneNumberStr is an uppercase letter, call the
method getNumber to determine the number and append (or concatenate) to
phoneNumber. Note that to check if the character is a letter consider the
ASCII range of uppercase letters.

For example, if(phoneNumberStr.charAt(i) == 65) is true when the character
at index i is A.

Also, recall that when considering characters use single quotes – ‘A’

b. If the current character of phoneNumberStr is NOT a letter, you will need to
create a one-character substring and then append/concatenate this substring to
phoneNumber

c. Output the phoneNumber

EXTRA CREDIT (2 points) Include appropriate hypens to the actual phone number.
That is, 1-800-flowers or 1800FLOWERS would display the phone number as
1-800-356-9377
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shaylen,

Welcome to CodeRanch!

Please bear in mind that this is NotACodeMill(<-click). It would be much better if you could tell us what have you done so far and what exact issues you are facing (maybe you can share few code snippets etc.).
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Start to write your algorithm out in pseudocode before coding.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint : it is not the place to ask others to do your work for you but I am sure you will get many help with your own code when you try.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!