0

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");
        }
    }
}
4
  • I don't know man but I think your code is a bit too complicated for no reason.Besides, I don't get the error you reported when I run the code. And what if s1.length does not equal s2.length? What happens then? what becomes of i and j? Commented Dec 13, 2014 at 20:31
  • If s1.length doesn't equals to s2.length then it should got to else block and then there is no need of i and j. I am still getting the same Exception, if you can give me any idea to change the method implementation then it would be very helpful Commented Dec 14, 2014 at 5:25
  • If s1.length doesn't equal s2.length, it doesn't get to the else block.The else block in your code belongs to another IF statement. Commented Dec 14, 2014 at 10:51
  • get rid of the char array completely Commented Dec 14, 2014 at 10:52

1 Answer 1

1

Your main() function passes in s1 and s3 , but your function has a hard-coded reference to arr2, which only has three elements. Any real implementation of a function like yours should create the char arrays dynamically, assuming that it's necessary to create those arrays at all.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Chris Long I found out the problem and rectified it. In the meantime can you please suggest what can I do if I have to compare to strings of different length, in my case s1 and s2, should I create any char array or not?
You do not need to create a char array. s1.length is 4.You do not need a char array to count the length of a string.
You don't ever need to create the arrays, because you can use the charAt method to access a String's characters, but then I assume that this is some sort of homework assignment, because otherwise you'd be using String.equals() instead of writing your own. If you're just testing for equality / non-equality, your initial comparison of the String's lengths is valid, and you only need to compare the characters of strings that are equal lengths. If you want to do a more complex comparison (deciding which sorts first alphabetically, for example), then you need to specify that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.