0

I am trying to write a method that will take a string, convert any letters to an int, and return all the converted ints to main, replacing the letters . I have if statements that convert all the letters to numbers, but I am having trouble making it work with a loop to convert all the letters instead of stopping after the first one. Any help would be appreciated, thanks in advance.

    public class PhoneNumberChecker
    {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        // Get the phone number
        System.out.print("Phone number to convert: ");
        String phoneNumber = input.nextLine();
        // Process each character in the phone number for display
        for (int i = 0; i < phoneNumber.length(); ++i)
        {
            // Get the character
            char ch = phoneNumber.charAt(i);
            if (Character.isLetter(ch))                         
                ch = (Character.toUpperCase(ch));               
            else
                System.out.print(ch);               
        }
        System.out.println(getNumber(phoneNumber));
        input.close();
        // end method

    }

    public static String getNumber(String phoneNumber)
    {

        for (int i = 0; i < phoneNumber.length(); ++i)
        {
            char ch = phoneNumber.charAt(i);
            ch = Character.toUpperCase(ch);

            if (ch == 'A' || ch == 'B' || ch == 'C')
                    return "2";         
                else if
                (ch == 'D' || ch == 'E' || ch == 'F')
                    return "3";
                else if
                (ch == 'G' || ch == 'H' || ch == 'I')
                    return "4";
                else if
                (ch == 'J' || ch == 'K' || ch == 'L')
                    return "5";
                else if
                (ch == 'M' || ch == 'N' || ch == 'O')
                    return "6";
                else if
                (ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S')
                    return "7";
                else if
                (ch == 'T' || ch == 'U' || ch == 'V')
                    return "8";
                else if
                (ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z')
                    return "9";

        }
        return "";



}
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
amoe
  • 3
  • 1
  • The loop is stopping because `return` terminates the loop. You probably want to store the numbers somewhere and then return that rather than return individual numbers in a loop (which doesn't make sense because it will always terminate on the first iteration) – chevybow Oct 09 '18 at 15:21
  • It sounds like you want to append the characters to a string and return the string after the loop, rather than return a single character. – David Oct 09 '18 at 15:22

4 Answers4

2

You want to append the string results to a string that will continue to grow as you iterate over the given phone number.

Create a String variable before your loop, then simply append to that string instead of returning the strings. Then once you're done iterating the phone number you can return the String.

public static String getNumber(String phoneNumber){

String convertedNum = "";
for (int i = 0; i < phoneNumber.length(); ++i)
    char ch = phoneNumber.charAt(i);
    ch = Character.toUpperCase(ch);

    if (ch == 'A' || ch == 'B' || ch == 'C')
        convertedNum  = convertedNum + "2"; //append to the string
    else if(ch == 'D' || ch == 'E' || ch == 'F')
        convertedNum  = convertedNum + "3";
    ...

return convertedNum; //then return it at the end
}
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • you're welcome! feel free to mark it as the answer in order to close the question. Good luck with the rest of your coding! – RAZ_Muh_Taz Oct 09 '18 at 16:02
0

You return from the method after the first character was handled. Let's modify your method:

public static String getNumber(String phoneNumber, int i)
{

    //for (int i = 0; i < phoneNumber.length(); ++i)
    {
        char ch = phoneNumber.charAt(i);
        ch = Character.toUpperCase(ch);

        if (ch == 'A' || ch == 'B' || ch == 'C')
                return "2";         
            else if
            (ch == 'D' || ch == 'E' || ch == 'F')
                return "3";
            else if
            (ch == 'G' || ch == 'H' || ch == 'I')
                return "4";
            else if
            (ch == 'J' || ch == 'K' || ch == 'L')
                return "5";
            else if
            (ch == 'M' || ch == 'N' || ch == 'O')
                return "6";
            else if
            (ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S')
                return "7";
            else if
            (ch == 'T' || ch == 'U' || ch == 'V')
                return "8";
            else if
            (ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z')
                return "9";

    }
    return "";
}

Note, that it has an int parameter and the cycle was commented out. Now, let's process a String:

public static function parseString(String input) {
    String output = "";
    for (int i = 0; i < input.length; i++) {
        output += getNumber(input, i);
    }
    return output;
}

Note, that this is very simple to understand. The thing which makes it simple is the fact that a method is doing a single thing. getNumber gets a number from a String at a given index. parseString parses the String in the way your code suggested. Of course you can modify the initial String if that is the purpose, using setChar, but then the getNumber method should return the char representation of the digits.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

As an alternative you could use String.relaceAll instead of checking each char in a nested if-else. Example:

public static String getNumber(String phoneNumber){
    String result = phoneNumber.toUpperCase()
            .replaceAll("[A-C]", "2")
            .replaceAll("[D-F]", "3")
            .replaceAll("[G-I]", "4")
            .replaceAll("[J-L]", "5")
            .replaceAll("[M-O]", "6")
            .replaceAll("[P-S]", "7")
            .replaceAll("[T-V]", "8")
            .replaceAll("[X-Z]", "9");
    return result;
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

I would suggest you to use StringBuilder as compared to String as it is preferable performance wise compared to String. The reason is String is immutable. So inside the loop the String object will be created again and again. Whereas StringBuilder is mutable so it is declared only once and then can be operated on by it's reference. You can use it as shown below:

public static String getNumber(String phoneNumber){

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < phoneNumber.length(); ++i){
        char ch = phoneNumber.charAt(i);
        ch = Character.toUpperCase(ch);

        if (ch == 'A' || ch == 'B' || ch == 'C')
            sb.append("2"); 
        else if(ch == 'D' || ch == 'E' || ch == 'F')
            sb.append("2");

        else if(ch == 'G' || ch == 'H' || ch == 'I')
            sb.append("3");
        else if(ch == 'J' || ch == 'K' || ch == 'L')
            sb.append("4");
        else if(ch == 'M' || ch == 'N' || ch == 'O')
            sb.append("5");
     }

     return sb.toString(); 
}

You can read about performance of String vs StringBuilder here. Pay attention to switch from concatination(+) to Builder.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52