简体   繁体   中英

How to loop a switch statement while using StringBuilder?

My current set of switch statements work, but they take up far too many lines and I am unsure of how to create a working loop to condense the switch statements.

Here is my current switch statement within a method (I have six, the following are incremented by 1)

public static String convert (String str) {

String strb = new StringBuilder(str);

switch (str.charAt(4)) {
            case 'a':
            case 'b':
            case 'c':
                strb.insert(4, 2);
                strb.deleteCharAt(5);
                break;
            case 'd':
            case 'e':
            case 'f':
                strb.insert(4, 3);
                strb.deleteCharAt(5);
                break;
            case 'g':
            case 'h':
            case 'i':
                strb.insert(4, 4);
                strb.deleteCharAt(5);
                break;
            case 'j':
            case 'k':
            case 'l':
                strb.insert(4, 5);
                strb.deleteCharAt(5);
                break;
            case 'm':
            case 'n':
            case 'o':
                strb.insert(4, 6);
                strb.deleteCharAt(5);
                break;
            case 'p':
            case 'q':
            case 'r':
            case 's':
                strb.insert(4, 7);
                strb.deleteCharAt(5);
                break;
            case 't':
            case 'u':
            case 'v':
                strb.insert(4, 8);
                strb.deleteCharAt(5);
                break;
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                strb.insert(4, 9);
                strb.deleteCharAt(5);
                break;
    }
     return strb.toString();
}

I have tried a for loop, but it does not seem to work. Any suggestions?

for (index = 4; index < str.length(); index++) {
     switch (str.charAt(index)) {
            case 'a':
            case 'b':
            case 'c':
                strb.insert(index, 2);
                strb.deleteCharAt(index + 1);
                break;
            case 'd':
            case 'e':
            case 'f':
                strb.insert(index, 3);
                strb.deleteCharAt(index + 1);
                break;
            case 'g':
            case 'h':
            case 'i':
                strb.insert(index, 4);
                strb.deleteCharAt(index + 1);
                break;
            case 'j':
            case 'k':
            case 'l':
                strb.insert(index, 5);
                strb.deleteCharAt(index + 1);
                break;
            case 'm':
            case 'n':
            case 'o':
                strb.insert(index, 6);
                strb.deleteCharAt(index + 1);
                break;
            case 'p':
            case 'q':
            case 'r':
            case 's':
                strb.insert(index, 7);
                strb.deleteCharAt(index + 1);
                break;
            case 't':
            case 'u':
            case 'v':
                strb.insert(index, 8);
                strb.deleteCharAt(index + 1);
                break;
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                strb.insert(index, 9);
                strb.deleteCharAt(index + 1);
                break;
     }
}

A better approach is to remove the switch statement altogether, and use a lookup table to select the digit:

private static final String DIGIT_LOOKUP = "22233344455566677778889999";
...
int pos = Character.toLowerCase(str.charAt(index)) - 'a';
char digit = DIGIT_LOOKUP.charAt(pos);

Demo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM