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

Thread: Palindromes?

  1. #1
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Palindromes?

    My problem is that tthe commented out replace all methods cause errors.
    /**
     * Write a description of class RecursivePalindrome here.
     * 
     * @author (Landon L) 
     * @version (12/7/13)
     */
    public class RecursivePalindrome
    {
        /**
         * Constructor for objects of class RecursivePalindrome
         */
        public RecursivePalindrome()
        {
        }
        public boolean isPalindrome(String word)
        {
            word = isPalindromeHelper(word);
            int a = word.length();
            if(a == 0 || a == 1)
                return true;
            if(word.charAt(0)== word.charAt(a-1))
               return isPalindrome(word.substring(1, a - 1));
            else
            return false;
        }
        public String isPalindromeHelper(String word)
        {
            word = word.toLowerCase();
            word = word.replaceAll(",","");
            word = word.replaceAll(".","");
            word = word.replaceAll("!","");
            word = word.replaceAll(" ","");
            word = word.replaceAll("\"\"","");
            word = word.replaceAll("'","");
            word = word.replaceAll(",","");
            //word = word.replaceAll("?","");
            //word.replaceAll("(","");
            //word.replaceAll(")","");
            return word;  
        }
    }
    import java.util.*;
    public class RecursivePalindromeTester
    {
        public static void main(String[] args)
        {
          RecursivePalindrome recP = new RecursivePalindrome();
          Scanner in = new Scanner(System.in);
          System.out.println("Type in word or phrase?(\"q\" to quit)");
          String word = in.next();
          if(word.charAt(0)!='q')
          {
            if(recP.isPalindrome(word))
               System.out.println("This is a palindrome.");
            else
               System.out.println("This isn't a palindrome.");
          }
          else
          {
          System.out.println("Thanks. Come again, later!");   
          System.exit(0);
          }
        }
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Palindromes?

    Quote Originally Posted by llowe29 View Post
            //word = word.replaceAll("?","");
            //word.replaceAll("(","");
            //word.replaceAll(")","");
    replaceAll works with a regular expression and in regexs '?', '(' and ')' are special characters with a specific meaning.

    It seems you are not interested in regexs, so use replace(CharSequence target, CharSequence replacement) (Java 5+) instead of replaceAll(). Don't misunderstand the name, replace replaces all occurrences. Simply it doesn't use the concept of regexs.

    But if you want to use regexs, you could also create a specific regex to match any of those single chars and replace them with empty string.

    Ah, and this:
    word = word.replaceAll(".","");

    doesn't do what you want. It is correct but '.' in regex means "any character".
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Palindromes?

    so example of implementation

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Palindromes?

    Quote Originally Posted by llowe29 View Post
    so example of implementation
    Sorry if I repeat myself, but it seems you don't have even basic knowledges on regexs, so my suggestion is to use replace instead of replaceAll.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Palindromes?

    thankks ill test it

    --- Update ---

    it works with word.replace("String", "With Str") no matter how many occurrences but I dont understand how(I mean I dont think it should work

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Palindromes?

    However, if you are curious and want to see a single regex to match all those chars, it's this:

    String s = "A,B.C!D E\"F'G?H(I)J";
    String s2 = s.replaceAll("[,.! \"'?()]", "");
     
    System.out.println(s);
    System.out.println(s2);

    The regex [ ] is a "character class" and matches any of the characters contained. So [abc] matches 'a' OR 'b' OR 'c'. The good thing of character classes is that many of special characters lose their specific meaning. So outside [ ] the '.' means "any character" while [.] means "the point".
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #7
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Palindromes?

    oh i see my teacehr never mentioned regex but showed us a way to remove all vowels. I didnt realize it worked for everything and could be changed.
    this is what I learned string.replaceAll("[aeiou]", "");

Similar Threads

  1. Finding out number of Palindromes in a Sentence
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 12th, 2010, 09:44 PM