Palindromes trouble.

mooo

thecyberninja
Joined
6 Jan 2004
Messages
886
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());
    }
}
 
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
 
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?
 
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.
 
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());


}
 
Where is your public void Palindrome(String someString) { // Rest of constructor goes here } inside class Palindrome.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,623
Latest member
AndersonLo
Back