Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Why am i getting the array bound of exception? im beginner in programming. Please help me understand. I have tried to convert the string to char array and reverse the characters and convert the end result back to string and compare with the original string

/* to check if the entered string is palinrome or not*/
class Palin
{
public static void main(String[] args)
{
String name="anna";
int l=name.length();
int d=l/2;
char tmp;
char tmp1;
char[] arr = name.toCharArray();

for(int j=0;j<d;j++)  /*to swap the characters*/
{
tmp=arr[j];
tmp1=arr[l];
arr[j]=tmp1;
arr[l]=tmp;
l=l-1;
}

String str=String.valueOf(arr); /* to convert the swapped char array back to string*/
if (str==name)
System.out.println("True");
else
System.out.println("False");

}
}
share|improve this question

3 Answers 3

up vote 0 down vote accepted

An array of N entries can be accessed with an index between 0 and N-1.

Change this:

tmp1 = arr[l];

To this:

tmp1 = arr[l-j-1];

And get rid of this:

l = l-1;

By the way, swapping can be done in 3 assignment operations instead of 4.

So you might as well change this:

tmp = arr[j];
tmp1 = arr[l];
arr[j] = tmp1;
arr[l] = tmp;
l = l-1;

To this:

tmp = arr[j];
arr[j] = arr[l-j-1];
arr[l-j-1] = tmp;
share|improve this answer

The reason you are getting the exception is because you initially set l = to name.length(). When you go to access the char array at l it is out of bounds because the array will hold 0 - name.length() - 1. You need to initialize l to l = name.length() - 1; Also don't use == to compare strings do str.equals(name).

public static void main(String[] args)
{
String name="anna";
**int l=name.length();**
int d=l/2;
char tmp;
char tmp1;
char[] arr = name.toCharArray();

for(int j=0;j<d;j++)  /*to swap the characters*/
{
tmp=arr[j];
**tmp1=arr[l];**
arr[j]=tmp1;
arr[l]=tmp;
l=l-1;
}

Here is fully functional code for your program:

public static void main(String[] args) {
        String name = "anna";
        int l = name.length();
        int d = l / 2;
        l = l - 1;
        char tmp;
        char tmp1;
        char[] arr = name.toCharArray();

        for (int j = 0; j < d; j++) /* to swap the characters */
        {
            tmp = arr[j];
            tmp1 = arr[l];
            arr[j] = tmp1;
            arr[l] = tmp;
            l = l - 1;
        }

        String str = String.valueOf(arr); /*
                                         * to convert the swapped char array
                                         * back to string
                                         */
        if (str.equals(name))
            System.out.println("True");
        else
            System.out.println("False");

    }
share|improve this answer
    
int l=name.length(); this will be storing the length of the string "anna" which will be 4. So, the part that does l=l-1 will have 3 and reduce one by one on each loop. This is my understanding. Can you please help me fix it? –  beginnr 20 hours ago
    
It will only reduce after you have already tried to access arr[4] which does not exist. arr[0-3] are valid indexes you don't do l = l - 1 until after you already access arr[4]. That is the problem but not the only problem with the code. Make sure to change str == name to str.equals(name). –  brso05 20 hours ago

brsp05 is right, your exception comes from the use of name.length() as the last index.

To avoid errors like this in the first place, you could avoid working with arrays here and use the StringBuilderclass instead. It provides a method to reverse a string:

String name = "anna";
String str = new StringBuilder(name).reverse().toString();

if (str.equals(name))
  System.out.println("True");
else
  System.out.println("False");

The usage of this method avoids a lot of unnecessary and error-prone code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.