I'm trying to search and reveal unknown characters in a string. Both strings are of length 12.
Example:
String s1 = "1x11222xx333";
String s2 = "111122223333"
The program should check for all unknowns in s1 represented by x|X and get the relevant chars in s2 and replace the x|X by the relevant char.
So far my code has replaced only the first x|X with the relevant char from s2 but printed duplicates for the rest of the unknowns with the char for the first x|X.
Here is my code:
String VoucherNumber = "1111x22xx333";
String VoucherRecord = "111122223333";
String testVoucher = null;
char x = 'x'|'X';
System.out.println(VoucherNumber); // including unknowns
//find x|X in the string VoucherNumber
for(int i = 0; i < VoucherNumber.length(); i++){
if (VoucherNumber.charAt(i) == x){
testVoucher = VoucherNumber.replace(VoucherNumber.charAt(i), VoucherRecord.charAt(i));
}
}
System.out.println(testVoucher); //after replacing unknowns
}
}
StringBuilder, which is mutable.char x = 'x'|'X';what are you trying to do here?charinside achar.'x' | 'X'evaluates to the bitwise-OR of the two char values'x'and'X', which equals120(or in char form, simply'x'). If you want to compare to two chars, you're gonna have to do two comparisons in yourifcondition.String.replace(char, char)doesn't do what you think it does; take a look at the docs. (It replaces all occurrences of that char, not just one)