Results 1 to 7 of 7
  1. #1
    AlfieSDK is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default 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!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    How could i compare different characters on an "int"
    There are no characters in an int. An int holds a numberic value.
    You can convert an int to a String by a method in the Integer class.
    Or the quick and dirty way: String intStr = "" + intVar;

  3. #3
    AlfieSDK is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you Norm, i will try it out.
    if not, then i'll come back.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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?

  6. #6
    AlfieSDK is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

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

    }
    }

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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)
    Java Code:
    Palindromes:
    ale
    1331
    16461
    So if you can turn the number into a string, you can simply reverse it and compare it to the original.

    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

  1. Replies: 12
    Last Post: 03-28-2011, 10:30 PM
  2. Replies: 10
    Last Post: 12-20-2010, 01:47 PM
  3. How to print PreparedStatment object in console
    By sasi.tati in forum AWT / Swing
    Replies: 1
    Last Post: 08-21-2010, 07:16 AM
  4. print random numbers without repetition
    By princess.blue in forum New To Java
    Replies: 3
    Last Post: 02-04-2010, 10:37 AM
  5. how to multiply numbers in rows and print it next to it
    By racewithferrari in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •