Reputation: 3
package palindrome;
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the word = > ");
String word = input.nextLine();
int flag = 0;
int x = word.length();
int i = 0;
int j = x;
for(; i<=x/2 && j>=x/2; i++,j--)
{
if(word.charAt(i)!=word.charAt(j))
{
flag = 1;
break;
}
}
if(flag==0)
{
System.out.printf("The word '%s' is a palindrome", word);
}
else
System.out.printf("The word '%s' is not a palindrome", word);
}
}
Enter the word = > madam
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:658)
at palindrome.Palindrome.main(Palindrome.java:16)
Upvotes: 0
Views: 164
Reputation: 1369
My suggestion is that you should have a seprate method to check for palindrome, This way you make your code clean and quality code.You can use below method to check for palindrome:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the word = > ");
String word = input.nextLine();
if(isPalindrome(word))
{
System.out.printf("The word '%s' is a palindrome", word);
}
else
System.out.printf("The word '%s' is not a palindrome", word);
}
private static boolean isPalindrome(String input) {
if(input == null || input.isEmpty()) {
return false;
}
for(int i=0,j=input.length() -1; i > j; i++,j-- ) {
if(input.charAt(i) != input.charAt(j)) {
return false;
}
}
return true;
}
Upvotes: 0
Reputation: 1
I got the mistake in your code.
"value of j = x;" // is wrong As String is a character array it starts with 0 to string.length-1.
thus int j = x-1 ; // will do right
Upvotes: 0
Reputation: 1124
Just to clarify the other answers:
A string is an array of chars, so if you write "madam"
[m][a][d][a][m] -> is 5 letters, so its length is 5
[0][1][2][3][4] -> but the indexes start with 0, so the last one is 4
Hence, the x=word.length()-1
Upvotes: 0
Reputation: 1893
you just forget to reduce the length of the word by 1: remember index of Strings and Arrays in Java begins from 0 so the length should always be minus 1 since it will not be reached. example if the length of your words is 4 you can get up to index 3 that is 0 to 3 makes total length of 4.
int x = word.length()-1;
int i = 0;
int j = x;
for (; i <= x / 2 && j >= x / 2; i++, j--) {
if (word.charAt(i) != word.charAt(j)) {
flag = 1;
break;
}
}
Upvotes: 0
Reputation: 162
Index is start from 0 in array.
package palindrome;
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the word = > ");
String word = input.nextLine();
int flag = 0;
int x = word.length();
int i = 0;
int j = x-1;
for(; i<=x/2 && j>=x/2; i++,j--)
{
System.out.println(i+" " +j );
if(word.charAt(i)!=word.charAt(j))
{
flag = 1;
break;
}
}
if(flag==0)
{
System.out.printf("The word '%s' is a palindrome", word);
}
else
System.out.printf("The word '%s' is not a palindrome", word);
}
}
Upvotes: 0
Reputation: 556
I suppose this happens because of the result of 5 divided to 2. Try keeping the middle length first as integer.
Upvotes: -2
Reputation: 1480
You have to set j's initial value to length-1
import java.util.Scanner;
public class Scratch
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the word = > ");
String word = input.nextLine();
int flag = 0;
int x = word.length()-1;
int i = 0;
int j = x;
for(; i<=x/2 && j>=x/2; i++,j--)
{
if(word.charAt(i)!=word.charAt(j))
{
flag = 1;
break;
}
}
if(flag==0)
{
System.out.printf("The word '%s' is a palindrome", word);
}
else
System.out.printf("The word '%s' is not a palindrome", word);
}
}
Upvotes: 1