0

The goal of the program is to make it take a phone number like 1-800Flower and make it change it into the number equivalent to a phone keypad like 1-8003569377 I can't seem to get the conversion from letter to number correct.

import java.util.Scanner;  
public class Phonekeypad 
{
  public static int getNumber (char uppercaseLetter)
  {  
     int k = 0;
     switch (uppercaseLetter) 
     {
        case 65: k = 2;
        break;
        case 66: k = 2;
        break;
        case 67: k = 2;
        break;
        case 68: k = 3;
        break;
        case 69: k = 3;
        break;
        case 70: k = 3;
        break;
        case 71: k = 4;
        break;
        case 72: k = 4;
        break;
        case 73: k = 4;
        break;
        case 74: k = 5;
        break;
        case 75: k = 5;
        break;
        case 76: k = 5;
        break;
        case 77: k = 6;
        break;
        case 78: k = 6;
        break;
        case 79: k = 6;
        break;
        case 80: k = 7;
        break;
        case 81: k = 7;
        break;
        case 82: k = 7;
        break;
        case 83: k = 7;
        break;
        case 84: k = 8;
        break;
        case 85: k = 8;
        break;
        case 86: k = 8;
        break;
        case 87: k = 9;
        break;
        case 88: k = 9;
        break;
        case 89: k = 9;
        break;
        case 90: k = 9;
        break;        
    }
     return k;
  }


public static void main(String[] args) 
{ 
     String s;
     System.out.println("Enter a string: ");
     Scanner input = new Scanner(System.in);
     s = input.next().toUpperCase();
     for (int i=0; i<s.length(); i++)
     {
         char c = s.charAt(i);
        // Is this a letter?
         if (Character.isLetter(c))
         { 
            int n;
            n = getNumber(c);
            char cn = 0;
            switch (n)
            {
             case 2: cn = '2';
             break;
             case 3: cn = '3';
             break;
             case 4: cn = '4';
             break;
             case 5: cn = '5';
             break;
             case 6: cn = '6';
             break;
             case 7: cn = '7';
             break;
             case 8: cn = '8';
             break;
             case 9: cn = '9';
             break;
            }
            s.replace(c, cn);
         }
     }
         System.out.println(s);
    }
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • There seems to be a problem with it, it doesn't want to convert the char part of the number. Like if I input 1-800Flowers it gives me back 1-800FLOWERS and the output I am trying to get is 1-8009569377 Im not sure what part of my program is faulty. – popcornhappy Nov 29 '12 at 22:17
  • 1-800Flower is 1-800356937 not 1-8003569377 – dreamcrash Nov 29 '12 at 22:21
  • Once you get this sorted out post your code on http://codereview.stackexchange.com/ and see how much cleaner this function could be :) – Jason Sperske Nov 29 '12 at 22:25
  • @user1864655 Did you manage to solve your question? – dreamcrash Dec 01 '12 at 21:57

2 Answers2

2

You need to change

   s.replace(c, cn);

to

 s = s.replace(c, cn);

Otherwise string s will not be updated. The reason behind this is that in Java all strings are immutable (i.e., they cannot change). Whenever you "change" a string what you are really doing is creating two strings and swapping their references. replace() is aware of this. And that is why the string passed as a parameter is not modified. Instead, replace() returns a new string that represents the string ´s´ with the characters replaced.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
0

Check out dreamcrash's solution. Unrelated, you could simplify some of your code by combining case statements. For example, instead of

case 65: k = 2;
break;
case 66: k = 2;
break;
case 67: k = 2;
break;

You could have

case 65:
case 66:
case 67: k = 2;
break;
Michael
  • 3,334
  • 20
  • 27