nzrr
nzrr

Reputation: 79

How to input String in Switch case containing charAt()

I want to append each number to represent the each alphabets and charAt() will not allow me to use a string case statement. Pls any ideas on how i can go about this?. I have already appended from alphabets to numbers. But from numbers to Alphabets, Its been something else for me. I really need help on this.

String Sentence;
    Sentence = JOptionPane.showInputDialog(null, "Enter some String", "Message");
    StringBuilder build = new StringBuilder();


    for(int i=0; i<trim.length();i++)
    {
for (int i = 0; i < Sentence.length(); i++) {
    switch (Sentence.charAt(i)) {
        case '1':
            build.append("a");
            break;
        case '2':
            build.append("b");
            break;
        case '3':
            build.append("c");
            break;
        case '4':
            build.append("d");
            break;
        case '5':
            build.append("e");
            break;
        case '6':
            build.append("f");
            break;
        case '7':
            build.append("g");
            break;
        case '8':
            build.append("h");
            break;
        case '9':
            build.append("i");
            break;
        case (
            '1.0'):
            build.append("j");
            break;
        case "1.1":
            build.append("k");
            break;
        case "1.2":
            build.append("l");
            break;
        case "1.3":
            build.append("m");
            break;
        case "1.4":
            build.append("n");
            break;
        case "1.5":
            build.append("o");
            break;
        case "1.6":
            build.append("p");
            break;
        case "1.7":
            build.append("q");
            break;
        case "1.8":
            build.append("r");
            break;
        case "1.9":
            build.append("s");
            break;
        case "2.0":
            build.append("t");
            break;
        case "2.1":
            build.append("u");
            break;
        case "2.2":
            build.append("v");
            break;
        case "2.3":
            build.append("w");
            break;
        case "2.4":
            build.append("x");
            break;
        case "2.5":
            build.append("y");
            break;
        case "2.6":
            build.append("z");
            break;
        case '?':
            build.append(" ");
            break;
        case '*':
            build.append(",");
            break;
        case "'\'":
            build.append('.');
            break;
        default:

            break;
    }

Seriously any help will be appreciated. Thanks

Upvotes: 0

Views: 716

Answers (1)

Ivan Pronin
Ivan Pronin

Reputation: 1906

To modify it quickly to working version - just convert your char to String and use it in switch:

 switch ("" + Sentence.charAt(i)) {
    case "a": // "a" is a String now
...

Upvotes: 1

Related Questions