Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

palindrome

Thread view: 
Benjamin Rushing - 02 Apr 2008 00:16 GMT
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?
Peter Duniho - 02 Apr 2008 00:29 GMT
> 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
Roedy Green - 02 Apr 2008 01:05 GMT
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<benrushing75@verizon.net> 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.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Eric Sosman - 02 Apr 2008 02:12 GMT
> 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.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Lord Zoltar - 02 Apr 2008 15:49 GMT
> > 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.
[quoted text clipped - 21 lines]
> Eric Sosman
> esos...@ieee-dot-org.invalid

Actually, I think I like this solution the best!
Logan Shaw - 02 Apr 2008 03:53 GMT
> 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
Roedy Green - 02 Apr 2008 15:36 GMT
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<benrushing75@verizon.net> 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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lord Zoltar - 02 Apr 2008 15:45 GMT
On Apr 1, 7:16 pm, "Benjamin Rushing" <benrushin...@verizon.net>
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. ;)
Roedy Green - 02 Apr 2008 16:55 GMT
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<benrushing75@verizon.net> 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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.