while( (rec=br.readLine()) != null)
{
for (int i=rec.length()-1;i > rec.length()-1; i--)
rString+=rec.charAt(i);
if (rec.equals(rString))
Problem with string not being compared
Page 1 of 113 Replies - 328 Views - Last Post: 30 March 2011 - 02:05 AM
#1
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.
Replies To: Problem with string not being compared
#2
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--)
#3
Re: Problem with string not being compared
Posted 26 March 2011 - 12:53 PM
Alex_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?
#4
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
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
#5
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.
#6
Re: Problem with string not being compared
Posted 28 March 2011 - 10:38 PM
sparkey77, 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.
#7
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(" ", "");
#8
Re: Problem with string not being compared
Posted 29 March 2011 - 03:11 PM
macosxnerd101, 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.
#9
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--)
should be
for (int i=rec.length()-1;i >= 0; i--)
This post has been edited by pbl: 29 March 2011 - 05:43 PM
#10
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()
#11
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.
#12
Re: Problem with string not being compared
Posted 29 March 2011 - 10:08 PM
macosxnerd101, 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.
#13
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;
}
#14
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!
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!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|