TOMER
TOMER

Reputation: 1

java.lang.StringIndexOutOfBoundsException: String index out of range when trying to check if a word is a palindrome

I been trying write code that checks if a word is a word that the first letter and the last letter are the same. In essence, it's code that checks to see if a Word is a palindrome.

Code

import java.util.*;

public class Class1 {
   static Scanner reader = new Scanner(System.in);

   public static void main(String[] args) {
       String n = reader.next();
       boolean right = true;
       for (int i=0; i<n.length();i++)
       {
           int f = n.length()-i;
           if (n.charAt(i) != n.charAt(f -i))
           {
               right=false;
           }
        
       }
       System.out.println("The word is " + right);
   }    
}

I get this error:

TT
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(Unknown Source)
at Class1.main(Class1.java:12)

Thank you.

Upvotes: 0

Views: 1252

Answers (5)

ThaBomb
ThaBomb

Reputation: 722

The error you are getting is an index out of range, stating your input parameter for n.charAt(#) is outside n's index range, it being from 0 to n.length()-1

The error in your code however is at this point of the code:

int f = n.length()-i; //negate i from the length
if (n.charAt(i) != n.charAt(f -i))  //negate i from the already negated length  

and the fix should be:

int f = n.length()-i-1;
if (n.charAt(i) != n.charAt(f))

Upvotes: 0

mirmdasif
mirmdasif

Reputation: 6354

To add with other answers you don't need to loop through the whole string. you only need to loop through half of the string length to see if your string is a palindrome or not.

Suppose you are checking if madam is palindrome or not. You have to loop half of the length of madam that is 5/2 or 2 times only.

index0 m == m index4

index1 a == a index2

So here is the slightly modified code

for(int i = 0;i<n.length()/2;i++) {
    if(n.charAt(i) != n.charAt(n.length()-1-i))
    {
        right=false;
    }
}

Upvotes: 0

SparkOn
SparkOn

Reputation: 8946

Suppose take an example the length of String is 3 and when i reaches 2 then according to you f will be 1

then take this line n.charAt(f -i) it will be like charAt(1-2) so definately it will throw some exception

Try something like this

    int s = n.length();
    for (int i=0; i<(s/2)+1;++i) {
        if (n.charAt(i) != n.charAt(s - i - 1)){
            right=false;
        }
    }
    System.out.println("The word is " + right);

Dont forget to debug the code to know about the flow only finding the solution is never going to help you

Upvotes: 0

kapex
kapex

Reputation: 29959

It's almost right, just int f = n.length()-i; should be int f = n.length()-1;.

n.length()-1 is the index of the last character in the string. So f-i will be the i-th character from the right.

Upvotes: 1

TDHofstetter
TDHofstetter

Reputation: 268

Try this:

import java.util.*;

public class Class1 {
   static Scanner reader = new Scanner(System.in);

   public static void main(String[] args) {
       String n = reader.next();
       boolean right = true;
       int f = n.length()-1;
       for (int i=0; i<n.length();i++)
       {
           if (n.charAt(i) != n.charAt(f-i))
           {
               right=false;
           }
       }
       System.out.println("The word is " + right);
   }    
}

Upvotes: 0

Related Questions