Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Help with String methods & forloops

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.).
 
Greenhorn
Posts: 29
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: Start to write your algorithm out in pseudocode before coding.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic