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 3 of 3

Thread: Palindrome program help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Palindrome program help

    I'm very new to programming and I'm in my first programming class. I was given a palindrome program and was asked to have the program test for palindromes while ignoring spaces and punctuation, I believe
    that I am on track to get this done but I don't know where I have gone wrong, does anyone have any suggestions?

    import java.util.Scanner;
     
    public class PalindromeTester
    {
       //-----------------------------------------------------------------
       //  Tests strings to see if they are palindromes.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          String str, another = "y";
          int left, right;
     
          Scanner scan = new Scanner (System.in);
     
          while (another.equalsIgnoreCase("y")) // allows y or Y
          {
             System.out.println ("Enter a potential palindrome:");
    		 String input = scan.nextLine();
             String strippedInput = input.replaceAll("\\W", "");
    		 str = scan.nextLine();
     
    		 }
     
    		 left = 0;
             right = str.length() - 1;
     
             while (str.charAt(left) == str.charAt(right) && left < right)
             {
                left++;
                right--;
             }
     
             System.out.println();
     
             if (left < right)
                System.out.println ("That string is NOT a palindrome.");
             else
                System.out.println ("That string IS a palindrome.");
     
             System.out.println();
             System.out.print ("Test another palindrome (y/n)? ");
             another = scan.nextLine();
          }
       }
    Last edited by timm1371; October 12th, 2011 at 08:48 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Palindrome program help

    Please do not duplicate threads...I have removed your other thread and moved this to a more appropriate location.

  3. #3

    Default Re: Palindrome program help

    String input = scan.nextLine();
    String strippedInput = input.replaceAll("\\W", "");
    str = scan.nextLine();
    What is the point of this code segment?

    After you ask for a palindrome, you assign it to strippedInput and then immediately expect more input. Why?

    while (str.charAt(left) == str.charAt(right) && left < right)
    {
    left++;
    right--;
    }


    System.out.println();

    if (left < right)
    System.out.println ("That string is NOT a palindrome.");
    else
    System.out.println ("That string IS a palindrome.");
    What if the string has an even number of chars? i.e., "pannap" that IS a palindrome
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. stack Palindrome , java
    By Faha in forum Object Oriented Programming
    Replies: 1
    Last Post: May 3rd, 2011, 04:20 PM
  2. Palindrome
    By mag12203 in forum Algorithms & Recursion
    Replies: 13
    Last Post: December 20th, 2010, 08:28 PM
  3. Palindrome Stacks
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 03:05 AM
  4. Check for palindrome numbers
    By SnooSnoo in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 4th, 2010, 06:11 PM

Tags for this Thread