Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Not getting desired result

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Not getting desired result

    //check number is palindrome or not.
    //not getting desired result
    import java.util.Scanner;
    class Palindrome

    {
    public static void main(String z[])
    {
    int rev=0;int a;
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int temp=n;
    while(n>0)
    {
    a=n%10;
    n=n/10;
    rev=rev*10+a;

    }
    if(n==rev)
    {
    System.out.println("pallindrome");
    }
    else{
    System.out.println("not a pallindrome");
    }
    }
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Not getting desired result

    I recommend you put in a print statement in your loop to print out the values of n, rev, and a to see what is happening.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not getting desired result

    try this:

    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String palString = String.valueOf(n);
    String reverse = new StringBuilder(palString).reverse().toString();
    if (palString.equals(reverse)) {
    System.out.println("pallindrome");
    } else {
    System.out.println("not a pallindrome");
    }

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Not getting desired result

    First, don't spoon feed solutions to OPs. Second, perhaps the OP wanted to figure out how to do it without using a reverse().

    Regards,
    Jim

Similar Threads

  1. commiting the transaction but not getting the desired results
    By Jack_Tauson_Sr in forum JDBC & Databases
    Replies: 0
    Last Post: April 10th, 2014, 01:54 AM
  2. [SOLVED] Code is not giving the desired output
    By Tree_Bek in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 07:27 PM
  3. No Error in Code but Output is not desired...!!!
    By Adi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 28th, 2011, 08:56 AM
  4. A few opinions on a project desired
    By LDM91 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2010, 02:36 PM
  5. HELP-WHY THE O/P IS NOT AS DESIRED.???
    By shreyash37 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 20th, 2010, 02:45 PM