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.Strin gIndexOutOfBoun dsException: String index out of range: 1
at java.lang.Strin g.charAt(String .javaL687)
at palindrome4.mai n(palindrome4.j ava: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.prin t( "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.prin tln ( "wOoOt! It's a PALINDROME!" );
else
System.out.prin tln (" Sorry! It's not a PALINDROME.");
Calendar date = Calendar.getIns tance();
System.out.prin tf( "\nProcesse d on %tc by \nElvar de los Santos\n", date );
}
}
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.Strin gIndexOutOfBoun dsException: String index out of range: 1
at java.lang.Strin g.charAt(String .javaL687)
at palindrome4.mai n(palindrome4.j ava: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.prin t( "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.prin tln ( "wOoOt! It's a PALINDROME!" );
else
System.out.prin tln (" Sorry! It's not a PALINDROME.");
Calendar date = Calendar.getIns tance();
System.out.prin tf( "\nProcesse d on %tc by \nElvar de los Santos\n", date );
}
}
Comment