beginnr
beginnr

Reputation: 5

Array index out of bound exception for palindrome java program

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");

}
}

Upvotes: 0

Views: 565

Answers (3)

Tom
Tom

Reputation: 17567

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.

Upvotes: 0

brso05
brso05

Reputation: 13222

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");

    }

Upvotes: 2

barak manos
barak manos

Reputation: 30136

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;

Upvotes: 0

Related Questions