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 :(

Answers


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'

Even though this already has been answered, I'd like to note a few things. Don't use an int or a long to save a phone number! You'll lose leading zeroes! Plus you'll easily go out of your int or long range. Plus the current accepted answer is a little harder to understand. I'd simply go with a lot less, and much easier to understand code:

public String toNormalPhoneNumber(String phoneNumber) {
    String normal = "";
    foreach (char c : phoneNumber.toUppercase().toCharArray())
        normal += getKeypadNumber(c);
    return normal;
}

public char getKeypadNumber(char characterToConvert) {
    if (Character.isDigit(characterToConvert))
        return characterToConvert;
    else {
        switch (characterToConvert) {
            case 'A' : case 'B' : case 'C' : return '2';
            case 'D' : case 'E' : case 'F' : return '3';
            case 'G' : case 'H' : case 'I' : return '4';
            case 'J' : case 'K' : case 'L' : return '5';
            case 'M' : case 'N' : case 'O' : return '6';
            case 'P' : case 'Q' : case 'R' : case 'S' : return '7';
            case 'T' : case 'U' : case 'V' : retrun '8';
            case 'W' : case 'X' : case 'Y' : case 'Z' : return '9';
            default return '?';
        }
    }
}

I reckon this is much easier to understand.


Here is a c# answer

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter the phone number (With Letters): ");
        string initial_phone_number = Console.ReadLine();
        initial_phone_number = initial_phone_number.ToUpper();
        string phone_number_final = full_number(initial_phone_number);

        Console.WriteLine("Output phone number for " + initial_phone_number + " is " + phone_number_final);
        Console.ReadLine();
    }

    public static string full_number(String initial_phone_number)
    {
        string number = "";
        string digit = "";
        int strLen = initial_phone_number.Length;

        for (int currCharacter = 0; currCharacter < strLen; currCharacter++)
        {
            string ch = initial_phone_number.Substring(currCharacter,1);
            int n;
            bool isNumeric = int.TryParse(ch, out n);
            if (!isNumeric)
            {
                switch (ch)
                {
                    case "A": case "B": case "C": digit = "2"; break;
                    case "D": case "E": case "F": digit = "3"; break;
                    case "G": case "H": case "I": digit = "4"; break;
                    case "J": case "K": case "L": digit = "5"; break;
                    case "M": case "N": case "O": digit = "6"; break;
                    case "P": case "Q": case "R": case "S": digit = "7";  break;
                    case "T": case "U": case "V": digit = "8";  break;
                    case "W": case "X": case "Y": case "Z": digit = "9";  break;

                }
                number = number + digit;
            }
            else if (isNumeric)
            {
                number = number + n.ToString();
            }
            else
            {
                Console.WriteLine("Invalid character!");
            }

        } 

        return number;

    }

}

Need Your Help

Sending JSON data from Express to Backbone

javascript json node.js backbone.js express

I made a simple Backbone app and I'm trying to use JSON data from a MYSQL database with an Express server.

How to split string by 'newline'?

android string split

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));