NameSilo

Java - Palindrom

Spaceship Spaceship
Watch

zk0

Established Member
Impact
3
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! :)

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.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
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
 
0
•••
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.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back