0

On your phone keypad and old ones, the alphabets are mapped to digits as follows: ABC(2), DEF(3), GHI(4), JKL(5), MNO(6), PQRS(7), TUV(8), WXYZ(9). Write a program which prompts user for a String (case insensitive), and converts to a sequence of Keypad digits.

public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);

    String str = scan.next().toLowerCase();

    for(int i = 0; i <= (str.length()-1); i++)
    {
        if(str.charAt(i) == 'A')
            System.out.print("2");
        else if(str.charAt(i) == 'B')
            System.out.print("22");
        else if(str.charAt(i) == 'C')
            System.out.print("222");
        else if(str.charAt(i) == 'D')
            System.out.print("3");
        else if(str.charAt(i) == 'E')
            System.out.print("33");
        else if(str.charAt(i) == 'F')
            System.out.print("333");
        else if(str.charAt(i) == 'G')
            System.out.print("4");
        else if(str.charAt(i) == 'H')
            System.out.print("44");
        else if(str.charAt(i) == 'I')
            System.out.print("444");
        else if(str.charAt(i) == 'J')
            System.out.print("5");
        else if(str.charAt(i) == 'K')
            System.out.print("55");
        else if(str.charAt(i) == 'L')
            System.out.print("555");
        else if(str.charAt(i) == 'M')
            System.out.print("6");
        else if(str.charAt(i) == 'N')
            System.out.print("66");
        else if(str.charAt(i) == 'O')
            System.out.print("666");
        else if(str.charAt(i) == 'P')
            System.out.print("7");
        else if(str.charAt(i) == 'Q')
            System.out.print("77");
        else if(str.charAt(i) == 'R')
            System.out.print("777");
        else if(str.charAt(i) == 'S')
            System.out.print("7777");
        else if(str.charAt(i) == 'T')
            System.out.print("8");
        else if(str.charAt(i) == 'U')
            System.out.print("88");
        else if(str.charAt(i) == 'V')
            System.out.print("888");
        else if(str.charAt(i) == 'W')
            System.out.print("9");
        else if(str.charAt(i) == 'X')
            System.out.print("99");
        else if(str.charAt(i) == 'Y')
            System.out.print("999");
        else if(str.charAt(i) == 'Z')
            System.out.print("9999");
    }
}

When I write a string, it's not giving me anything! Just blank. What seems to be the problem?

Kj45
  • 79
  • 2
  • 2
  • 11

2 Answers2

4

Here is your culprit:

String str = scan.next().toLowerCase();

Change it to:

String str = scan.next().toUpperCase();
Simon DeWolf
  • 400
  • 3
  • 10
0
  1. You need to repair the code:

    String str = scan.next().toLowerCase();
    

to:

    String str = scan.next().toUpperCase();
  1. If you don't wanna use a lot of if-then like that, you can use this:

    private int takeResult(char c){
    int toInt = (int) c;
    if(c < 'A' && c > 'Z') throw new IllegalArgumentException();
    if (c == 'S') return 7777;
    if (c == 'Z') return 9999;
    int magic = toInt % 3;
    if (c > 'S') return (((toInt / 3) - 20) * (45 * magic * magic - 35 * magic + 1));
    return ((toInt / 3) - (((-1) * magic * magic + magic + 40) / 2)) * ((-105) * magic * magic + 205 * magic + 11);
    }
    

Though it's more sophisticated but more "fun" :)). GLHF!!

SalaryNotFound
  • 224
  • 1
  • 8