Ned help in PALINDROME

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Synapse
    New Member
    • Dec 2006
    • 26

    Ned help in PALINDROME

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

    }
    }
  • Vikram Sharma
    New Member
    • Jul 2007
    • 3

    #2
    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.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Synapse
      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 );

      }
      }
      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.

      Comment

      Working...