I have written this code to find the common prefix for 3 Strings. However, it gives outOfBond Exception in the 14.line ( in the first while statement ). Could you please help to solve this problem?
import java.util.*;
public class Main{
public static void main (String[] args){
Scanner scanner = new Scanner(System.in);
String str1 = scanner.next();
String str2 = scanner.next();
String str3 = scanner.next();
int i = 0;
int j = 0;
while (Character.compare(str1.charAt(i), str2.charAt(i)) == 0){
i++;
}
String subStr1 = str1.substring(0,i);
while (Character.compare(subStr1.charAt(j), str3.charAt(j)) == 0){
j++;
}
String subStr2 = subStr1.substring(0,j);
System.out.println(subStr2);
}}