Palindromes trouble.

Discussion in 'Web Design & Coding' started by mooo, Jan 24, 2005.

  1. mooo

    mooo thecyberninja

    Messages:
    886
    Location:
    earth
    I am trying to do a palindrome lab. I have it setup but now i am having trouble on how to output the string with out spaces once it has checked. Any idea?

    here is what i have gotten so far.


    Code:
    public class Palindrome {
        private String pal;
        
        public Palindrome(String initPal) {
    	pal = initPal.toUpperCase();
        }
        
        public boolean isPalindrome() {
           
    	if (pal.length() <= 1) {
    	    // String has only one character so it
    	    // is a Palindrome by definition.
    	    return true;		// BASE CASE.
    	}
    	
    	// Get the first and last characters of the String.
    	char first = pal.charAt(0);
    	char last = pal.charAt(pal.length()-1);
    	
    	if (Character.isLetter(first) &&
    	    Character.isLetter(last)) {
    	    // The first and last characters are both letters..
    
    	    if (first != last) {
    		// The first and last letters are different
    		// so the string is not a Palindrome.
    		return false;           // BASE CASE.
    	    }
    	    else {
    		// The first and last characters are both letters,
    		// and they are both the same. So, the string is 
    		// a palindrome if the substring created by dropping
    		// the first and last characters is a palindrome.
    		Palindrome sub = new Palindrome(
    			         pal.substring(1,pal.length()-1));
    		return sub.isPalindrome();  // RECURSIVE CASE.
    	    }
    	}
    	else if (!Character.isLetter(first)) {
    	    // The first character in the string is not a letter.
    	    // So the string is a palindrome if the substring created
    	    // by dropping the first character is a palindrome.
    	    Palindrome sub = new Palindrome(pal.substring(1));
    	    return sub.isPalindrome();      // RECURSIVE CASE.
    	}
    	else {
    	    // The last character in the string is not a letter.
    	    // So the string is a palindrome if the substring created
    	    // by dropping the last character is a palindrome.
    	    Palindrome sub = new Palindrome(
                                 pal.substring(0,pal.length()-1));
    	    return sub.isPalindrome();      // RECURSIVE CASE.
    	}
        }
    
        public static void main(String[] args) {
    	Palindrome p1 = new Palindrome("Madam, I'm Adam.");
    	System.out.println(p1.isPalindrome());
    	Palindrome p2 = new Palindrome("Nope!");
    	System.out.println(p2.isPalindrome());
    	Palindrome p3 = new Palindrome("dad");
    	System.out.println(p3.isPalindrome());
    	Palindrome p4 = new Palindrome("Go hang a salami, I'm a lasagna hog.");
    	System.out.println(p4.isPalindrome());
        }
    }
     
  2. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    To me it looks like right now it is just going to output TRUE and FALSE right? you don't seem to be returning any characters to System.out.println, so how can it print anything at all?

    X-Istence
     
  3. mooo

    mooo thecyberninja

    Messages:
    886
    Location:
    earth
    yea. i wasn't sure on how i would go about doing that. I know when i return i can't return the string i don't think. Is there a way to do this?
     
  4. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Code:
    //
    //  RandomInt.java
    //  Project 4
    //
    //  Created by Jan-Willem on Thu Jan 9 2005.
    //  Copyright (c) 2005 X-Istence.com All rights reserved.
    //
    
    import java.util.Random;
    
    public class RandomInt {
            Random Randomater;
            
            public RandomInt() {
                    Randomater = new Random();
            }
            
            public int getNumberInRange(int aLowerLimit, int aUpperLimit) {
                    if (aLowerLimit >= aUpperLimit) {
                            StringBuffer message = new StringBuffer();
                            message.append("Lower limit (");
                            message.append(aLowerLimit);
                            message.append(") must be lower than Upper limit (");
                            message.append(aUpperLimit);
                            message.append(")");
                            throw new IllegalArgumentException(message.toString());
                    }
                    
                    // get the range, casting to long to avoid overflow problems
                    long range = (long)aUpperLimit - (long)aLowerLimit + 1;
                    // compute a fraction of the range, 0 <= frac < range
                    long fraction = (long)(range * Randomater.nextDouble());
                    return (int)(fraction + aLowerLimit);
            }
    }
    
    Look at the way StringBuffer works, then figure out how you can use it in your application.
     
  5. mooo

    mooo thecyberninja

    Messages:
    886
    Location:
    earth
    Ok, i redid my code :)
    But i can't figure out why my main won't compile, i am getting this error

    Main.java:14: cannot find symbol
    symbol : constructor Palindrome(java.lang.String)
    location: class Palindrome
    Palindrome p5 = new Palindrome("001010 151010101");


    (for all my p1-p7)
    --------code starts here---------------

    public class Palindrome
    {
    public static boolean isPalindrome(String word)
    {
    int left = 0; // index of leftmost unchecked char
    int right = word.length() -1; // index of the rightmost
    while (left < right)
    { // continue until they reach center
    if (word.charAt(left) != word.charAt(right))
    {
    return false; // if chars are different, finished
    }
    left++; // move left index toward the center
    right--; // move right index toward the center
    }
    return true; // if finished, all chars were same
    }


    public static boolean isSubPal3(String word)
    {
    int subleft = 0;
    int subright = 3;
    while (subleft < subright)
    { // continue until they reach center
    if (word.charAt(subleft) != word.charAt(subright))
    {
    return false; // if chars are different, finished
    }
    subleft++; // move left index toward the center
    subright--; // move right index toward the center
    }
    return true; // if finished, all chars were same
    }

    public static boolean isSubPal4 (String word)
    {
    int subleft = 0;
    int subright = 4;
    while (subleft < subright)
    { // continue until they reach center
    if (word.charAt(subleft) != word.charAt(subright))
    {
    return false; // if chars are different, finished
    }
    subleft++; // move left index toward the center
    subright--; // move right index toward the center
    }
    return true; // if finished, all chars were same
    }
    }


    and my main


    public class Main extends Object
    {
    public static void main(String[] args)
    {
    Palindrome p1 = new Palindrome("11123turrut3211");
    System.out.println(p1.isPalindrome());
    Palindrome p2 = new Palindrome("Able was I ere I saw Elba.");
    System.out.println(p2.isPalindrome());
    Palindrome p3 = new Palindrome("Madam, I'm, Adam.");
    System.out.println(p3.isPalindrome());
    Palindrome p4 = new Palindrome("a man, a plan, a canal -- panama!");
    System.out.println(p4.isPalindrome());
    Palindrome p5 = new Palindrome("001010 151010101");
    System.out.println(p5.isPalindrome());


    }
     
  6. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Where is your public void Palindrome(String someString) { // Rest of constructor goes here } inside class Palindrome.