NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Java - Palindrom

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 12-12-2006, 05:25 AM THREAD STARTER               #1 (permalink)
zk0
NamePros Member
 
zk0's Avatar
Join Date: Jun 2005
Posts: 163
zk0 is an unknown quantity at this point
 



Help! Java - Palindrom


I need some help with this Java code. It's supposed to be a code that says if a text is a palindrom or not (I have no idea what's its called in english). A palindrom is a text that will be spelled the same if it's read backwards (=adolf i paris rapar sirap i floda).

This is the start code:

Code:
package your_username;

public class Step3_Lab02
{
	public static void main(String[] args)
	{
	}
}

class Palindrome
{
	public boolean isPalindrome(String str)
	{
		String reverseStr = (new StringBuffer(str)).reverse().toString();
		return str.equalsIgnoreCase(reverseStr);
	}

	public String removeNoneLetters(String str)
	{
		String result = "";
		for (int i = 0; i < str.length(); ++i)
		{
			if (Character.isLetter(str.charAt(i)))
			{
				result = result + str.charAt(i);
			}
		}

		return result;
	}
}
I am only allowed to add new code in the main-method:
Code:
	public static void main(String[] args)
	{
	}
I've already added a Scanner to the code. It now loks like this:
Code:
package your_username;

import java.util.Scanner;

public class Step3_lab02
{

}

class Palindrome
{
	public boolean isPalindrome(String str)
	{
		String reverseStr = (new StringBuffer(str)).reverse().toString();
		return str.equalsIgnoreCase(reverseStr);
	}

	public String removeNoneLetters(String str)
	{
		String result = "";
		for (int i = 0; i < str.length(); ++i)
		{
			if (Character.isLetter(str.charAt(i)))
			{
				result = result + str.charAt(i);
			}
		}
		return result;
	}
}
If you think you can help me please post!
????: NamePros.com http://www.namepros.com/programming/268148-java-palindrom.html

I have problem with knowing how to delete all the signs that is not letters in the text I write.

And Im a little unsure how to use the method "nextLine" in the Scanner-class.
zk0 is offline  
Old 12-15-2006, 05:43 PM   #2 (permalink)
NamePros Member
 
mr_love_monkey's Avatar
Join Date: Oct 2004
Posts: 40
mr_love_monkey is an unknown quantity at this point
 



Why do you need to use 'Scanner' - how are you planning on using this program?

Are you just going to give it a possible palindrome at the command line?

Surely all you would need to do in your main is

Code:
public static void main(String [] args) {
Palindrome p = new Palindrome();
String possiblePal = args[0];
possiblePal = p.removeNoneLetters(possiblePal);
if(p.isPalindrome(possiblePal))
   System.out.println("Is palindrome");
else
   System.out.println("Is not palindrome");
}
which expects your possible palindrome to be the first argument when you call the code
mr_love_monkey is offline  
Old 12-17-2006, 09:18 AM   #3 (permalink)
Senior Member
 
legend2's Avatar
Join Date: Sep 2005
Posts: 1,112
legend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud oflegend2 has much to be proud of
 



Well think of it logically.
take the text from command line, through args[].

Then, start 2 indexed, one at 0 and one equal to the length of the string.
The first index moves forward, the second one backward.
Keeping incrementing the first and decrementing the second as long as the characters match at both indexes. yous top when 2 characters are not the same, or when the 2 pointers reach the same index or the first bypasses the second.
legend2 is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 03:27 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger