10 Replies - 5686 Views - Last Post: 26 March 2010 - 04:45 AM Rate Topic: -----

#1 aletty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 25-March 10

how to ignore case and certain characters

Posted 25 March 2010 - 07:29 PM

I'm trying to create a method that will test if a String is a palindrome or not. So far, I have that part working. What I'm trying to do now is have the method ignore the case of the String. I tried using .equalsIgnoreCase() but have not been successful so far.

Also, how might one go about also ignoring certain characters?


Thanks very much.


This is what I have as far as working code at this point:

public static boolean palindrome(String inputText)
{

boolean isPalindrome = false;

for (int i=0; i<inputText.length()/2; i++)

{
if (inputText.charAt(i) == inputText.charAt(inputText.length()-1 -i))

{
isPalindrome = true;

}
else
{
return false;
}
}
return isPalindrome;
}

Is This A Good Question/Topic? 0
  • +

Replies To: how to ignore case and certain characters

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: how to ignore case and certain characters

Posted 25 March 2010 - 07:38 PM

To do a case insensitive comparison, I recommend a call to toLowerCase()
inputText = inputText.toLowerCase();


Also to ignore a character, I would use the continue; statement.
if(inputText.charAt(i) == 'a')
{
    continue;
}


The above could be applied in anyway of course. Basically this just skips that iteration of the loop and goes to the next character.
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: how to ignore case and certain characters

Posted 25 March 2010 - 07:40 PM

Use a local string variable to convert the input string to upper case:

...
public static boolean palindrome(String inputText) {

    String text = inputText.toUpperCase();

    boolean isPalindrome = false;

...



See String toUpperCase()
Was This Post Helpful? 0
  • +
  • -

#4 aletty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 25-March 10

Re: how to ignore case and certain characters

Posted 25 March 2010 - 07:55 PM

Thanks!

What about ignoring a '? It doesn't allow me to do

if (inputText.charAt(i) == ''')
{
continue;
}
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to ignore case and certain characters

Posted 25 March 2010 - 08:01 PM

View Postaletty, on 25 March 2010 - 08:55 PM, said:

Thanks!

What about ignoring a '? It doesn't allow me to do

if (inputText.charAt(i) == ''')
{
continue;
}

Ignoring a what ?
if (inputText.charAt(i) == '?')
or
if (inputText.charAt(i) == '''')
I guess 2 '' to make a single '
Was This Post Helpful? 0
  • +
  • -

#6 aletty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 25-March 10

Re: how to ignore case and certain characters

Posted 25 March 2010 - 08:06 PM

Sorry, I'm trying to ignore a single apostrophe. No number of them seems to work, whether it's 1, 2, 3 or 4 of them.

This post has been edited by aletty: 25 March 2010 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: how to ignore case and certain characters

Posted 25 March 2010 - 08:15 PM

Try this:

...
if (inputText.charAt(i) == '\'') // escape the apostrophe
...


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to ignore case and certain characters

Posted 25 March 2010 - 08:18 PM

View Postaletty, on 25 March 2010 - 09:06 PM, said:

Sorry, I'm trying to ignore a single apostrophe. No number of them seems to work, whether it's 1, 2, or 3 of them.

Just mixed up between C and Java here
if you where comparing String you can do "\'"
never tried '\'' ... actually I am interested to know if that one works

worst case use if (inputText.charAt(i) == 39)

View Postn8wxs, on 25 March 2010 - 09:15 PM, said:

Try this:

...
if (inputText.charAt(i) == '\'') // escape the apostrophe
...


Thanks n8wxs I was almots sure it would work

This post has been edited by pbl: 25 March 2010 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: how to ignore case and certain characters

Posted 25 March 2010 - 08:20 PM

It works: :)

String xxx = "this is a test ' ' ' ";

for (int i = 0; i < xxx.length(); i++)
{
    if (xxx.charAt(i) == '\'')
        System.out.println("yes");

}



Output:

Quote

yes
yes
yes
BUILD SUCCESSFUL (total time: 3 seconds)

Was This Post Helpful? 0
  • +
  • -

#10 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: how to ignore case and certain characters

Posted 25 March 2010 - 10:39 PM

Remember that chars are declared as such:
char ch = 'a';

Whereas Strings are declared as:
String str = "Text Here";

Notice the ' versus the ".
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2698
  • View blog
  • Posts: 11,367
  • Joined: 20-September 08

Re: how to ignore case and certain characters

Posted 26 March 2010 - 04:45 AM

Try
word = word.replaceAll("\\W", "");
	boolean isPalindrome = (word.equalsIgnoreCase(new StringBuilder(word).reverse().toString()));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1