HELP FAST turning a phone number into digits

create a class named Phone which will encapsulate a phone number enter

Page 1 of 1

2 Replies - 1129 Views - Last Post: 11 November 2009 - 09:36 AM Rate Topic: -----

#1 steph89  Icon User is offline

  • New D.I.C Head

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

HELP FAST turning a phone number into digits

Post icon  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  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 285
  • View blog
  • Posts: 1,619
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • 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:
:code:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1