This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Palindrome Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Palindrome" Watch "Palindrome" New topic


Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
Ok I have a quick question. I am working on an assignment for class and one of the methods I am using is pretty simple. I am using JOptionPane to have a user input a positive value. What I am wondering is if there is a way to distinquish how many digits are in that number that can be known in the next method. I will outline that I am trying to do so that this might make sense:

First I will write a method that takes an integer number as input argument and returns the number of digits in that number.

Next I will write a method that takes an integer number and an integer array argument and fills the array with the appropriate digits of the number. So for example it would set intArray[0] to the value 8, intArray[1] to 7, ..., and intArray[4] to 2.

Third I will write a boolean method that takes an integer array argument and returns the value true if the array is a palindrome else returns false.

Last I will write the main method that prompts the user repeatedly for an integer number, that uses the first method to find out the number of digits in that number, allocates the int array for that size, calls the second method to populate the array with the digits of the number, calls the third method to find out whether the array is a palindrome, and then prints out the result. The program will terminate when the user enters -99.

So ovbiously I do not know how to do all of this yet, but I am working on it. I was just wondering if anyone had any tips about the array and the first method in which I must find out how many digits the value is that the user entered. I think if I understood that I might understand the rest of the assignment a bit more. I feel a little overwhelmed and I promise I did not put all of this on here for someone to do it for me, I just thought it might help for you all to understand where my question was coming from. I appreciate it. Thanks!
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
1) Check if the user entered value meets your criteria - i.e a positive number
2) As the user entered value is a string before it is converted to a number you can always get the length of the string using user_string.length()
3) Look at API

It should even help you get an array from the user input string.

Hope this helps
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The length of the string the user enters is a good starting point, but may give you funny answers. How many digits does "004" have? Since the question is about integers, the answer may be "1". How about "-5"? or "_2_" with blanks where I put underscores? If you want to get elaborate, you might:

Whew! Does that help?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Holly Leery
Ranch Hand

Joined: Sep 08, 2004
Posts: 97
ok reverting back above to the boolean expression. I am a little confused there....

If palindrome is usually used for letters and really is not supposed to be used for numbers i dont understand how it can be put into java. I mean how would there be a code for that? Can I use an if else statement that some how has the int Array[] = to the reverse of that int?? Little lost....any comments?
It is sorta covered in the JavaRanch Style Guide.
subject: Palindrome
It's not a secret anymore!