palindrome

Discussion in 'Java' started by Benjamin Rushing, Apr 2, 2008.

  1. I've been tasked with writing a simple java program where the user inputs a
    5 digit number and the program states whether it's a palindrome or not.
    I can't use an array or use the 'reverse' function of code. Anybody willing
    to give me a hint on how to procede?
    Benjamin Rushing, Apr 2, 2008
    #1
    1. Advertising

  2. Benjamin Rushing

    Peter Duniho Guest

    On Tue, 01 Apr 2008 16:16:49 -0700, Benjamin Rushing
    <> wrote:

    > I've been tasked with writing a simple java program where the user
    > inputs a
    > 5 digit number and the program states whether it's a palindrome or not.


    Tasked by whom? A teacher assigning homework?

    > I can't use an array or use the 'reverse' function of code.


    Does the person assigning the task consider a string to be an array? How
    about other collection types? Do they mean that you're only prohibited
    from using things that are literally arrays? Or is any collection of data
    that uses array-like semantics a problem for some reason?

    > Anybody willing to give me a hint on how to procede?


    It seems to me that the easiest way is to take the input data, and
    iterative compare pairs of characters from the beginning and ending of the
    string. If you reach the middle of the string and every character
    matched, then it's a palindrome. If you find two characters that don't
    match, it's not.

    As long as you are allowed to use a string in your processing, the above
    should be simple enough. Between the String class, a for() loop, and an
    if() that controls the flow for each character comparison, you've got
    everything you need.

    Pete
    Peter Duniho, Apr 2, 2008
    #2
    1. Advertising

  3. Benjamin Rushing

    Roedy Green Guest

    On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
    <> wrote, quoted or indirectly quoted someone
    who said :

    > Anybody willing
    >to give me a hint on how to procede?


    you will use a loop that will chase left to right and compute the
    corresponding right to left char.

    Draw some on paper, writing down the indexes of the characters.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Apr 2, 2008
    #3
  4. Benjamin Rushing

    Eric Sosman Guest

    Benjamin Rushing wrote:
    > I've been tasked with writing a simple java program where the user inputs a
    > 5 digit number and the program states whether it's a palindrome or not.
    > I can't use an array or use the 'reverse' function of code. Anybody willing
    > to give me a hint on how to procede?


    class DetectPalindrome {
    public static void main(String[] args) {
    int number = Integer.parseInt(args[0]);
    System.out.print(number + " is ");
    if (number < 0)
    System.out.println("not a palindrome.");
    else
    System.out.println("a palindrome "
    + " when written in base "
    + Math.max(number + 1L, 2));
    }
    }

    There are, I suppose, less elegant approaches involving
    the % operator, or conversion to String form and comparing
    the results of carefully-chosen charAt() methods. Ho-hum.

    --
    Eric Sosman
    lid
    Eric Sosman, Apr 2, 2008
    #4
  5. Benjamin Rushing

    Logan Shaw Guest

    Benjamin Rushing wrote:
    > I've been tasked with writing a simple java program where the user inputs a
    > 5 digit number and the program states whether it's a palindrome or not.

    ^^^^^^^^^^^^^^

    Guaranteed to be exactly 5 digits? Use this to your advantage!

    - Logan
    Logan Shaw, Apr 2, 2008
    #5
  6. Benjamin Rushing

    Roedy Green Guest

    On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I've been tasked with writing a simple java program where the user inputs a
    >5 digit number and the program states whether it's a palindrome or not.
    >I can't use an array or use the 'reverse' function of code. Anybody willing
    >to give me a hint on how to procede?


    Mathematician's algorithm:

    generate all possible palindromes. Put them in an HashSet.

    Look up the candidate. If it in is the set it is a Palindrome.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Apr 2, 2008
    #6
  7. Benjamin Rushing

    Lord Zoltar Guest

    On Apr 1, 7:16 pm, "Benjamin Rushing" <>
    wrote:
    > I've been tasked with writing a simple java program where the user inputs a
    > 5 digit number and the program states whether it's a palindrome or not.
    > I can't use an array or use the 'reverse' function of code.  Anybody willing
    > to give me a hint on how to procede?


    First, calculate the probability that any given 5 digit number COULD
    be a palindrome.
    Then ignore the input and output yes or no with the same probability
    that the input MIGHT be a palindrom.
    For very large datasets, this might be sufficient! ;)


    seriously... if it's ALWAYS going to be 5 characters then it's very
    easy: compare 1st and last character, 2nd and 3rd character. you could
    say:
    (input[0]==input[4] && input[1]==input[3]) => palindrome
    you will have to find some way to split up your input string... If
    you're not sure how then check through Java's string library. There's
    several ways to do this. ;)
    Lord Zoltar, Apr 2, 2008
    #7
  8. Benjamin Rushing

    Lord Zoltar Guest

    On Apr 1, 9:12 pm, Eric Sosman <> wrote:
    > Benjamin Rushing wrote:
    > > I've been tasked with writing a simple java program where the user inputs a
    > > 5 digit number and the program states whether it's a palindrome or not.
    > > I can't use an array or use the 'reverse' function of code.  Anybody willing
    > > to give me a hint on how to procede?

    >
    >         class DetectPalindrome {
    >             public static void main(String[] args) {
    >                 int number = Integer.parseInt(args[0]);
    >                 System.out.print(number + " is ");
    >                 if (number < 0)
    >                     System.out.println("not a palindrome.");
    >                 else
    >                     System.out.println("a palindrome "
    >                         + " when written in base "
    >                         + Math.max(number + 1L, 2));
    >             }
    >         }
    >
    >      There are, I suppose, less elegant approaches involving
    > the % operator, or conversion to String form and comparing
    > the results of carefully-chosen charAt() methods.  Ho-hum.
    >
    > --
    > Eric Sosman
    >


    Actually, I think I like this solution the best!
    Lord Zoltar, Apr 2, 2008
    #8
  9. Benjamin Rushing

    Roedy Green Guest

    On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I've been tasked with writing a simple java program where the user inputs a
    >5 digit number and the program states whether it's a palindrome or not.
    >I can't use an array or use the 'reverse' function of code. Anybody willing
    >to give me a hint on how to procede?


    Slacker's solution:

    Reading the number as a string is no problem. It is extra work to read
    it as an int.

    See http://mindprod.com/applet/fileio.html

    reverse the string. Compare.

    To reverse the string without using reverse, cheat, and see how
    reverse does it by looking at the code in source.zip

    But the point is to LEARN java, not create a palindrome detector, so
    this is a stupid solution.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Apr 2, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lorin Leone

    Palindrome (HELP)

    Lorin Leone, Nov 12, 2003, in forum: C++
    Replies:
    4
    Views:
    1,009
    Chris Theis
    Nov 13, 2003
  2. Runic911

    Palindrome

    Runic911, Nov 13, 2003, in forum: Python
    Replies:
    24
    Views:
    1,595
    Andrew Dalke
    Nov 15, 2003
  3. Tim Churches

    Re: Re: Palindrome

    Tim Churches, Nov 13, 2003, in forum: Python
    Replies:
    2
    Views:
    739
    yousafzai
    Jun 5, 2011
  4. Pierre Quentel

    Re: Palindrome

    Pierre Quentel, Nov 13, 2003, in forum: Python
    Replies:
    2
    Views:
    511
    Francis Avila
    Nov 13, 2003
  5. cat_dog_ass

    Palindrome using StringBuffer

    cat_dog_ass, Jan 23, 2007, in forum: Java
    Replies:
    4
    Views:
    2,936
    abhi2varma
    Jan 5, 2013
Loading...

Share This Page