-1

When I run my code after I input this

4 qwertyuiopasdfghjklzxcvbnm
Ph
Pcssi
Bpke_kdc_epclc_jcijsc_mihyo?
Ipp!

output

Hg
Hmllg
Vham akm mhmlm hmghlm dggng?
Ghh!

when it should output

Hi
Hello
What are these people doing?
Ohh!




public static void main(String[] args) 
{

    String input="";
    int cases= sc.nextInt();
    String al= sc.next();
    String upAl= al.toUpperCase();
    char [] upAlph = upAl.toCharArray();


    char[] alph = al.toCharArray();
    for(int i=0; i<cases; i++)
    {
        input=sc.next();





        input=input.replace("a", ""+ alph[0]);
        input=input.replace("b", ""+ alph[1]);
        input=input.replace("c", ""+ alph[2]);
        input=  input.replace("d", ""+ alph[3]);
        input=  input.replace("e", ""+ alph[4]);
        input=  input.replace("f", ""+ alph[5]);
        input=  input.replace("g", ""+ alph[6]);
        input=  input.replace("h", ""+ alph[7]);
        input=  input.replace("i", ""+ alph[8]);
        input=  input.replace("j", ""+ alph[9]);
        input=  input.replace("k", ""+ alph[10]);
        input=input.replace("l", ""+ alph[11]);
        input=input.replace("m", ""+ alph[12]);
        input=input.replace("n", ""+ alph[13]);
        input=  input.replace("o", ""+ alph[14]);
        input=input.replace("p", ""+ alph[15]);
        input=  input.replace("q", ""+ alph[16]);
        input=input.replace("r", ""+ alph[17]);
        input=  input.replace("s", ""+ alph[18]);
        input=input.replace("t", ""+ alph[19]);
        input=input.replace("u", ""+ alph[20]);
        input=input.replace("v", ""+ alph[21]);
        input=input.replace("w", ""+ alph[22]);
        input=input.replace("x", ""+ alph[23]);
        input=input.replace("y", ""+ alph[24]);
        input=input.replace("z", ""+ alph[25]);
        input=input.replace("A", upAlph[0]+"");
        input=input.replace("B", upAlph[1]+"");
        input=input.replace("C", upAlph[2]+"");
        input=input.replace("D", upAlph[3]+"");
        input=input.replace("E", upAlph[4]+"");
        input=input.replace("F", upAlph[5]+"");
        input=input.replace("G", upAlph[6]+"");
        input=input.replace("H", upAlph[7]+"");
        input=input.replace("I", upAlph[8]+"");
        input=input.replace("J", upAlph[9]+"");
        input=input.replace("K", upAlph[10]+"");
        input=input.replace("L", upAlph[11]+"");
        input=input.replace("M", upAlph[12]+"");
        input=input.replace("N", upAlph[13]+"");
        input=input.replace("O", upAlph[14]+"");
        input=input.replace("P", upAlph[15]+"");
        input=input.replace("Q", upAlph[16]+"");
        input=input.replace("R", upAlph[17]+"");
        input=input.replace("S", upAlph[18]+"");
        input=input.replace("T", upAlph[19]+"");
        input=input.replace("U", upAlph[20]+"");
        input=input.replace("V", upAlph[21]+"");
        input=input.replace("W", upAlph[22]+"");
        input=input.replace("X", upAlph[23]+"");
        input=input.replace("Y", upAlph[24]+"");
        input=input.replace("Z", upAlph[25]+"");
        input=input.replace("_", " ");

        pw.println(input);

    }
}
Will Jamieson
  • 918
  • 1
  • 16
  • 32
  • sc, al what are they? – Kent Mar 02 '13 at 00:40
  • @WillJamieson forgot to comment that you will get this behavior under [your previous question](http://stackoverflow.com/questions/15167986/replaceall-is-not-replacing-the-substrings). – jlordo Mar 02 '13 at 00:43

1 Answers1

4

The problem is that you are replacing letters that have already being replaced on each subsequent call of String#replace, effectively jumbling up the letters. You would need to mark the positions of the letters that have already been replaced.

In cases like these I use StringUtils, e.g. (Caps omitted):

StringUtils.replaceChars("ph", "abcdefghijklmnopqrstuvwxyz", "qwertyuiopasdfghjklzxcvbnm")

Output:

hi
Reimeus
  • 158,255
  • 15
  • 216
  • 276