471,122 Members | 1,153 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

Ned help in PALINDROME

26
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it.

If I enter 33633, 11211, 45554, it will return

It's a Palindrome!

and if I enter 33636 or 11214, it returns

It's not a Palindrome!

now my problem is if i enter 00000, it got some errors the one below:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.javaL687)
at palindrome4.main(palindrome4.java:19)

And if I only enter 1,2,3 or 4 digits like 33 or 112, i got the same error.

String index out of range: 1 or 2 or 3 or 4 (depending on the input)

Here's my code below..


import java.util.*;

public class palindrome4
{
public static void main (String args[])
{

Scanner input = new Scanner (System.in );
String num
int number, num1, num2, num3, num4, num5;

System.out.print( "Enter a 5-digit integer: " );
number = input.nextInt();

len = number + "";
num1 = num.charAt(0);
num2 = num.charAt(1);
num3 = num.charAt(2);
num4 = num.charAt(3);
num5 = num.charAt(4);

if (num1 == num5 && num2 == num4 )
System.out.println ( "wOoOt! It's a PALINDROME!" );
else
System.out.println (" Sorry! It's not a PALINDROME.");

Calendar date = Calendar.getInstance();
System.out.printf( "\nProcessed on %tc by \nElvar de los Santos\n", date );

}
}
Jul 10 '07 #1
2 2525
You should check if the length of the input string is equal to 5.
if yes only then you should move foward.
you are extracting charAt upto 5 chars so it will obviously give the problem of out of bound.
Jul 10 '07 #2
r035198x
13,262 8TB
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it.

If I enter 33633, 11211, 45554, it will return

It's a Palindrome!

and if I enter 33636 or 11214, it returns

It's not a Palindrome!

now my problem is if i enter 00000, it got some errors the one below:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.javaL687)
at palindrome4.main(palindrome4.java:19)

And if I only enter 1,2,3 or 4 digits like 33 or 112, i got the same error.

String index out of range: 1 or 2 or 3 or 4 (depending on the input)

Here's my code below..


import java.util.*;

public class palindrome4
{
public static void main (String args[])
{

Scanner input = new Scanner (System.in );
String num
int number, num1, num2, num3, num4, num5;

System.out.print( "Enter a 5-digit integer: " );
number = input.nextInt();

len = number + "";
num1 = num.charAt(0);
num2 = num.charAt(1);
num3 = num.charAt(2);
num4 = num.charAt(3);
num5 = num.charAt(4);

if (num1 == num5 && num2 == num4 )
System.out.println ( "wOoOt! It's a PALINDROME!" );
else
System.out.println (" Sorry! It's not a PALINDROME.");

Calendar date = Calendar.getInstance();
System.out.printf( "\nProcessed on %tc by \nElvar de los Santos\n", date );

}
}
Remember
1.) The program only does what you tell it to do.
2.) String characters indexing start at 0
3.) Java also has a convenient for loop.
4.) You can wrap your code in code tags when posting it so it's easier to read.
Jul 10 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Lorin Leone | last post: by
23 posts views Thread by Amar Prakash Tripaithi | last post: by
32 posts views Thread by ramakrishnadeepak | last post: by
4 posts views Thread by outofmymind | last post: by
3 posts views Thread by colinNeedsJavaHelp | last post: by
4 posts views Thread by vicestner | last post: by
5 posts views Thread by rubyhuang | last post: by
2 posts views Thread by bigtd08 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.