This is your assignment inside your for loop
: -
char c = to.charAt(from.indexOf(line.charAt(i)));
Here, in indexOf
returns -1
when the char
is not found in from
string, and then it will throw an StringIndexOutOfBoundsException
.
You can add a check before fetching the character: -
int index = from.indexOf(line.charAt(i));
if (index >= 0) {
char c = to.charAt(index);
outPutText += c;
}
or: -
char ch = line.charAt(i);
if (from.contains(ch)) {
char c = to.charAt(from.indexOf(ch));
outPutText += c;
}