13 Replies - 425 Views - Last Post: 30 March 2011 - 02:05 AM Rate Topic: -----

#1 sparkey77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-February 11

Problem with string not being compared

Posted 25 March 2011 - 10:25 PM

I dont understand why it is compiling clean and running but it will not say that one or any line is a palindrome. Do I need another for loop or is it not reversing the string and then comparing the original string? Any thoughts would be great thanks.
 while( (rec=br.readLine()) != null)
        {
          for (int i=rec.length()-1;i > rec.length()-1; i--)
                rString+=rec.charAt(i);

          if (rec.equals(rString))


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with string not being compared

#2 Alex_H  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 27-January 11

Re: Problem with string not being compared

Posted 25 March 2011 - 10:40 PM

One problem is with your for loop. You start out with the length of the string. You then loop while the length of the string is greater then the length of the string. The second length should be 0.


for (int i=rec.length()-1;i > 0; i--)



Was This Post Helpful? 1
  • +
  • -

#3 sparkey77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-February 11

Re: Problem with string not being compared

Posted 26 March 2011 - 12:53 PM

View PostAlex_H, on 25 March 2011 - 11:40 PM, said:

One problem is with your for loop. You start out with the length of the string. You then loop while the length of the string is greater then the length of the string. The second length should be 0.


for (int i=rec.length()-1;i > 0; i--)



I changed the for loop like you said but it still makes no diffrence to the out come of the while loop and always shows the strings not to be a palindrome. Do I need to nest another while loop inside my first while loop or do I need another for loop?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,654
  • Joined: 27-December 08

Re: Problem with string not being compared

Posted 26 March 2011 - 12:58 PM

Please post ALL relevant code, not just this excerpt.

What I would do when testing a String to see if it is palindromic is:
-Remove the whitespace (see the String replace() method)
-Set the String toLowerCase() or toUpperCase()
-Pass the String to a StringBuilder, and invoke the StringBuilder reverse() method
-Compare your String after removing whitespace and case differences to the toString() of the StringBuilder
Was This Post Helpful? 1
  • +
  • -

#5 sparkey77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-February 11

Re: Problem with string not being compared

Posted 26 March 2011 - 04:18 PM

Yes I would love to use the revese method but in this we cant. That it is why I am trying to use a for loop to reverse the string I called rec coming in, but I not sure if it is not fully reversing the string or if it is even touching the string rec. I will put in a system.out line to check wht that for loop is doing maybe that will help.
Was This Post Helpful? 0
  • +
  • -

#6 sparkey77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-February 11

Re: Problem with string not being compared

Posted 28 March 2011 - 10:38 PM

View Postsparkey77, on 26 March 2011 - 05:18 PM, said:

Yes I would love to use the revese method but in this we cant. That it is why I am trying to use a for loop to reverse the string I called rec coming in, but I not sure if it is not fully reversing the string or if it is even touching the string rec. I will put in a system.out line to check wht that for loop is doing maybe that will help.

Well I put in a test line and it, is reversing the full line and I have added a replace method to take out the most the punctuation marks and made the line all lowercase. Now need to take out all the white space and get the program to compare the line one at a time.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,654
  • Joined: 27-December 08

Re: Problem with string not being compared

Posted 29 March 2011 - 05:52 AM

You can use the String replace() method again to remove whitespace. Like so:
String update = str.replace(" ", "");


Was This Post Helpful? 0
  • +
  • -

#8 sparkey77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-February 11

Re: Problem with string not being compared

Posted 29 March 2011 - 03:11 PM

View Postmacosxnerd101, on 29 March 2011 - 06:52 AM, said:

You can use the String replace() method again to remove whitespace. Like so:
String update = str.replace(" ", "");


I am useing or trying to use the replace method
rec=rec.replace('.',' ');
but it does not remove all the symbols I need removed and it replaces it with a space. If I use the
rec=rec.replace(" ","");
it gives me an error of missing character. I will keep trying and hopefully it will come to me thanks.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

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

Re: Problem with string not being compared

Posted 29 March 2011 - 05:27 PM

for (int i=rec.length()-1;i > rec.length()-1; i--)

should be

for (int i=rec.length()-1;i >= 0; i--)

This post has been edited by pbl: 29 March 2011 - 05:43 PM

Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Problem with string not being compared

Posted 29 March 2011 - 05:39 PM

For stripping leading and trailing whitespace, it's esaiest to use String.trim()
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,654
  • Joined: 27-December 08

Re: Problem with string not being compared

Posted 29 March 2011 - 09:27 PM

Agreed. But for palindromes, removing only leading and trailing whitespace isn't always enough. :)
Was This Post Helpful? 0
  • +
  • -

#12 sparkey77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-February 11

Re: Problem with string not being compared

Posted 29 March 2011 - 10:08 PM

View Postmacosxnerd101, on 29 March 2011 - 10:27 PM, said:

Agreed. But for palindromes, removing only leading and trailing whitespace isn't always enough. :)

Ok I got it to work I used a series of replaceAll() to take out white spaces and punctation and reset my rString back to "" so it would not append when getting compared again after my if statements. Thanks for all your help.
Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1948
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Problem with string not being compared

Posted 30 March 2011 - 01:51 AM

If all you want to do is check if it's a palindrome, you don't need to create a reverse string at all. Just check if the first character is equal to the last, then check if the second is equal to the second last, etc. Something like this:

boolean isPalindrome(String s) {
  int low = 0;
  int high = s.length() - 1;
  while(low < high) {
    if (s.charAt(low) != s.charAt(high)) {
      return false;
    }
    low ++;
    high --;
  }
  return true;
}

Was This Post Helpful? 1
  • +
  • -

#14 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1948
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Problem with string not being compared

Posted 30 March 2011 - 02:05 AM

Instead of a series of replaceAll, you can use a single regex. In regex, you can specify character groups with square brackets. So [ ] will mathc a space, [ ,.!"$%^&*()] will match a space or any of the punctuation marks. So you could build the replaceAll like this:

s = s.replaceAll("[ ,.!\"$%^&*()]", "");

That's pretty powerful but we can do better. There are a lot of symbols not included in that regex and it would take too long to fix it!

You can also specify negative character classes with a ^. So, [^a-z] matches any character that is not a lower case letter. To filter out all non-letters, you can do something like this:

s = s.replaceAll("[^a-zA-Z]", "");

Enjoy!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1