When I run my code after I input this
1 qwertyuiopasdfghjklzxcvbnm
will
it still prints out will instead of replacing the characters like it should, why is that?
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.replaceAll("a", ""+ alph[0]);
input.replaceAll("b", ""+ alph[1]);
input.replaceAll("c", ""+ alph[2]);
input.replaceAll("d", ""+ alph[3]);
input.replaceAll("e", ""+ alph[4]);
input.replaceAll("f", ""+ alph[5]);
input.replaceAll("g", ""+ alph[6]);
input.replaceAll("h", ""+ alph[7]);
input.replaceAll("i", ""+ alph[8]);
input.replaceAll("j", ""+ alph[9]);
input.replaceAll("k", ""+ alph[10]);
input.replaceAll("l", ""+ alph[11]);
input.replaceAll("m", ""+ alph[12]);
input.replaceAll("n", ""+ alph[13]);
input.replaceAll("o", ""+ alph[14]);
input.replaceAll("p", ""+ alph[15]);
input.replaceAll("q", ""+ alph[16]);
input.replaceAll("r", ""+ alph[17]);
input.replaceAll("s", ""+ alph[18]);
input.replaceAll("t", ""+ alph[19]);
input.replaceAll("u", ""+ alph[20]);
input.replaceAll("v", ""+ alph[21]);
input.replaceAll("w", ""+ alph[22]);
input.replaceAll("x", ""+ alph[23]);
input.replaceAll("y", ""+ alph[24]);
input.replaceAll("z", ""+ alph[25]);
input.replaceAll("A", upAlph[0]+"");
input.replaceAll("B", upAlph[1]+"");
input.replaceAll("C", upAlph[2]+"");
input.replaceAll("D", upAlph[3]+"");
input.replaceAll("E", upAlph[4]+"");
input.replaceAll("F", upAlph[5]+"");
input.replaceAll("G", upAlph[6]+"");
input.replaceAll("H", upAlph[7]+"");
input.replaceAll("I", upAlph[8]+"");
input.replaceAll("J", upAlph[9]+"");
input.replaceAll("K", upAlph[10]+"");
input.replaceAll("L", upAlph[11]+"");
input.replaceAll("M", upAlph[12]+"");
input.replaceAll("N", upAlph[13]+"");
input.replaceAll("O", upAlph[14]+"");
input.replaceAll("P", upAlph[15]+"");
input.replaceAll("Q", upAlph[16]+"");
input.replaceAll("R", upAlph[17]+"");
input.replaceAll("S", upAlph[18]+"");
input.replaceAll("T", upAlph[19]+"");
input.replaceAll("U", upAlph[20]+"");
input.replaceAll("V", upAlph[21]+"");
input.replaceAll("W", upAlph[22]+"");
input.replaceAll("X", upAlph[23]+"");
input.replaceAll("Y", upAlph[24]+"");
input.replaceAll("Z", upAlph[25]+"");
input.replaceAll("_", " ");
pw.println(input);
}
Strings are
immutable
in java , you cannot modify strings after creating them, consider assigning the result ofreplaceAll
to the original stringThe method
replaceAll
returns a new string after replacement.even
+=
doesn't add the value to the string, it creates another string internally.Thus, consider usingStringBuilder
for better performance if you want to modify the string very oftenI was coding this method by Friday, before leaving from job, but had to leave.
Anyhow, I did finish it today.
How to test it? :
Strings
are immutable. Assigninput
to the result ofreplaceAll
:Aside: Consider using String#replace if regular expressions are not required.