# HELP FAST turning a phone number into digits

Page 1 of 1

## 2 Replies - 1578 Views - Last Post: 11 November 2009 - 09:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=138313&amp;s=da1f98f18f2574da3d3a0eec4ec77bed&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 steph89

Reputation: 0
• Posts: 2
• Joined: 11-November 09

# HELP FAST turning a phone number into digits

Posted 11 November 2009 - 08:09 AM

Need to use a swithc statement instead of else if needhelp???? due in 3 hours!

changed it but cannot find the substring (int int) that is the error i get on line 20

public class phone
{
private String number;
private String word;

public phone (String word)
{
this.word=word;
setNumber();
}

private void setNumber()
{
number = "";

for (int i = 0; i < word.length() && i < number.length(); i +=1)
{
if (number.length() ==3)
{
number = number.subString(0,2) + "-" + word.subString(3);
}

char letter = word.charAt(i);
word.toUpperCase();
switch (number)
{
case 'A':
case 'B':
case 'C':
number += "2";
break;
case 'D':
case 'E':
case 'F':
number += "3";
break;
case 'G':
case 'H':
case 'I':
number += "4";
break;
case 'J':
case 'K':
case 'L':
number += "5";
break;
case 'M':
case 'N':
case 'O':
number += "6";
break;
case 'P':
case 'R':
case 'S':
number += "7";
break;
case 'T':
case 'U':
case 'V':
number += "8";
break;
case 'W':
case 'X':
case 'Y':
number += "9";
break;
case '-':
break;
default:
System.out.print ("Invalid input.");
}
}

}

public String toString()
{
return letter;
}
}

This post has been edited by steph89: 11 November 2009 - 08:59 AM

Is This A Good Question/Topic? 0

## Replies To: HELP FAST turning a phone number into digits

### #2 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,620
• Joined: 15-May 08

## Re: HELP FAST turning a phone number into digits

Posted 11 November 2009 - 08:43 AM

What i think you are intending to do is check a character at each index of the String. You would do that via the following.

```String number ="";

String s = "irepresentphonenumber";

for ( int i = 0; i < s.length; i++ )
{
if ( s.charAt( i ) == 'A' || s.charAt( i ) == 'B' || s.charAt( i ) == 'C' )
number +="1";
if ( s.charAt( i ) == 'D' || s.charAt( i ) == 'E' || s.charAt( i ) == 'F' )
number +="2";
// ... etc.

}
```

gl

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12316
• Posts: 45,416
• Joined: 27-December 08

## Re: HELP FAST turning a phone number into digits

Posted 11 November 2009 - 09:36 AM

@Bbq:: I think the OP needs to use a switch statement.

@OP: Please use code tags in the future when posting code. They are used as follows:

In terms of the program, you are calling the substring() method with the second 's' capitalized like so subString(). This method's name is completely lowercased.

Also, you may want to switch the order of these two statements:
```char letter = word.charAt(i);
word.toUpperCase();

```

So that letter isn't assigned 'a' instead of 'A'. Lastly, you need the variable letter in the switch statement, not number. Remember, switch statements are limited to variables of types byte, short, int and char.

And lastly, in your toString() method, you should be getting an error b/c it can't access the variable letter. This variable is local to your for-loop in your setNumber() method.