John Sideris John Sideris - 3 months ago 5
Java Question

Convert Letters & Numbers in a Phone Number to all Numbers (Java)

import java.util.Scanner;

import javax.swing.JOptionPane;


public class PhonePadTranslator {

private static Scanner input;

public static void main(String[] args) {

input = new Scanner(System.in);
System.out.println("Enter The Phone Number (With Letters) ");
String initial_phone_number = input.nextLine();
initial_phone_number = initial_phone_number.toUpperCase();
int phone_number_final = 0;

System.out.printf("The phone number for %s is %s", initial_phone_number, phone_number_final);

}//end of main

public static int full_number(String initial_phone_number)
{
int which_character = 0;
int phone_number_final = 0;
char ch = (Character) null;

for (which_character = 0; which_character < initial_phone_number.length(); which_character++)
{
if (Character.isLetter(ch))
{
switch(ch)
{
case 'A' : case 'B' : case 'C' : phone_number_final = 2; break;
case 'D' : case 'E' : case 'F' : phone_number_final = 3; break;
case 'G' : case 'H' : case 'I' : phone_number_final = 4; break;
case 'J' : case 'K' : case 'L' : phone_number_final = 5; break;
case 'M' : case 'N' : case 'O' : phone_number_final = 6; break;
case 'P' : case 'Q' : case 'R' : case 'S' : phone_number_final = 7; break;
case 'T' : case 'U' : case 'V' : phone_number_final = 8; break;
case 'W' : case 'X' : case 'Y' : case 'Z' : phone_number_final =9; break;
}
return (char)phone_number_final;
}
if (Character.isDigit(ch))
{
return (char)phone_number_final;
}

else {
return (char)phone_number_final;
}

} //end of for
return ch;
}//end of full_number
}//end of class


I just thought I'd copy/paste the whole thing... but whenever I run the code, it keeps outputting The phone number for 1800FLOWERS is 0. Now I'm sure there's some other things that are wrong, but my main concern is why it keeps giving me a
0
? I feel like it's because I initialised it to that and for some reason I'm never changing the value. Please help, my professor takes forever to respond to my emails :(

Answer

Change

int phone_number_final = 0;

to

int phone_number_final = full_number(initial_phone_number);


You did not assign the result to your variable.

Other than that, I believe your full_number function is not exactly correct either.


Updated code:

import java.util.Scanner;

public class StringToNumbers
{
    private static Scanner input;

    public static void main(String[] args)
    {
        input = new Scanner(System.in);
        System.out.println("Enter The Phone Number (With Letters): ");
        String initial_phone_number = input.nextLine();

        initial_phone_number = initial_phone_number.toUpperCase();
        long phone_number_final = full_number(initial_phone_number);

        System.out.printf("%nOutput phone number for '%s' is '%s'",
                initial_phone_number, phone_number_final);
    }

    public static long full_number(String initial_phone_number)
    {
        // Use long instead of int for 'number' if the string will be longer than max int value
        // 2147483647, which is '10 digits'
        long number = 0;
        int strLen = initial_phone_number.length();


        for (int currCharacter = 0; currCharacter < strLen; currCharacter++) 
        {
            char ch = initial_phone_number.charAt(currCharacter);
            // For A-Z & 0-9, multiply by 10, add the 'char' to number.
            // i.e., Shift existing value to the left by 1 digit, add current 'char' to it
            // Use long instead of int if the string will be longer than max int value (2147483647)

            if (Character.isLetter(ch)) 
            {
                switch(ch)
                {
                case 'A' : case 'B' : case 'C' : number *= 10; number += 2; break;
                case 'D' : case 'E' : case 'F' : number *= 10; number += 3; break;
                case 'G' : case 'H' : case 'I' : number *= 10; number += 4; break;
                case 'J' : case 'K' : case 'L' : number *= 10; number += 5; break;
                case 'M' : case 'N' : case 'O' : number *= 10; number += 6; break;
                case 'P' : case 'Q' : case 'R' : case 'S' : number *= 10; number += 7; break;
                case 'T' : case 'U' : case 'V' : number *= 10; number += 8; break;
                case 'W' : case 'X' : case 'Y' : case 'Z' : number *= 10; number += 9; break;
                }
            }
            else if (Character.isDigit(ch))
            {
                number *= 10; number += Character.getNumericValue(ch);
            }
            else
            {
                System.out.println("Invalid character!");
            }

        } // End of for loop

        // Return actual number only at the end of the function
        return number;

    }// End of full_number function    
}


Input/Output:

Enter The Phone Number (With Letters): 
1800FLOWERS

Output phone number for '1800FLOWERS' is '18003569377'