I have written the below program to compare two strings, but I am getting ArrayIndexOutOfBoundException. I don't understand why. Please review the code below and help me. Thanks in advance:
public class IPrepTest4 { static String s1 = "Java"; static String s2 = "Jav"; static String s3 = "Java"; static boolean b = false; static char[] arr1 = s1.toCharArray(); static char[] arr2 = s2.toCharArray(); static char[] arr3 = s3.toCharArray(); public static void main(String[] args) { //compareString(s1,s2); compareString(s1,s3); } public static void compareString(String s1, String s2) { if (s1.length() == s2.length()) { int i = 0; int j = 0; while (i<s1.length() && j<s2.length()) { b = (arr1[i] == arr2[j]); i++;j++; } if (b) System.out.println("String s1: " + s1 + " and String s2: " + s2 + " are equal"); } else { System.out.println("String s1: " + s1 + " and String s2: " + s2 + " are not equal"); } } }