Results 1 to 7 of 7
- 05-19-2011, 08:45 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
How to print palindromic numbers on the console!
Hi!
I'm trying to make the console print only palindromic 4 digit numbers, but i can't find a way. I do know how to do it with one number in particular like "2002", where i use :
String palindrome = "2002";
if (palindrome.charAt(0) == palindrome.charAt(3)
&& palindrome.charAt(1) == palindrome.charAt(2)) {
System.out.println("is a palindrome");
}
But i can't do it like that, because i'm using a increment cycle, so it's an "int" and not a String like the case above....
How could i compare different characters on an "int"?
or maybe i got it all wrong.
I'm kinda lost....
Thank you!
- 05-19-2011, 11:11 PM #2
There are no characters in an int. An int holds a numberic value.How could i compare different characters on an "int"
You can convert an int to a String by a method in the Integer class.
Or the quick and dirty way: String intStr = "" + intVar;
- 05-20-2011, 03:35 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Thank you Norm, i will try it out.
if not, then i'll come back.
- 05-20-2011, 03:42 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, what makes a number(or word) a palindrome? Think about this and see if there is a method in the string(or stringXxx) class to accomplish this.
I leave it intentionally vague, when/if you realize what I'm hinting at it becomes incredibly simple to find if a number(or word) is a palindrome. You will also be able to test numbers(or words) where the length is x and x is an unknown.
- 05-20-2011, 04:23 AM #5
Do you talk of finding a palindrome for int values? If you convert the int to a String, what base do you use?
Or do you look at the int value in hex? or binary?
- 05-21-2011, 01:23 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
I think I did it....
Thank you guys!
I just needed to print 4 digit palindromes... :D
public class capicuaParcial {
public static void main(String[] args) {
for (int contador = 1000; contador < 10000; contador++) {
System.out.println(contador);
String numerosEnLetras = contador + "";
if (numerosEnLetras.charAt(0) == numerosEnLetras.charAt(3)
&& numerosEnLetras.charAt(1) == numerosEnLetras.charAt(2)) {
System.out.println(contador+" es Capicua");
}
}
}
}
- 05-21-2011, 02:24 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Glad you solved it.
As to my approach I was hinting at(in case you need to find out if less than or more than a 4 digit(letter) word is palindrome) was this.
A word, or number is a palindrome if it can be spelled the same forwards and backwards(the key word here is backwords)
So if you can turn the number into a string, you can simply reverse it and compare it to the original.Java Code:Palindromes: ale 1331 16461
You can do it the naive way, and loop through the string backwards constructing it in a stringbuilder. The smart, and simpler way would be to simply reverse it with a method call, then compare them with equals.
Similar Threads
-
to print first and last number from a set of input numbers
By 95673641 in forum New To JavaReplies: 12Last Post: 03-28-2011, 09:30 PM -
Program - Print row of 5 even numbers, 5 rows, last num 50
By An Alien in forum New To JavaReplies: 10Last Post: 12-20-2010, 12:47 PM -
How to print PreparedStatment object in console
By sasi.tati in forum AWT / SwingReplies: 1Last Post: 08-21-2010, 06:16 AM -
print random numbers without repetition
By princess.blue in forum New To JavaReplies: 3Last Post: 02-04-2010, 09:37 AM -
how to multiply numbers in rows and print it next to it
By racewithferrari in forum New To JavaReplies: 1Last Post: 01-16-2010, 06:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks