Palindromes

Reply

Join Date: Dec 2008
Posts: 11
Reputation: javafrustrated is an unknown quantity at this point 
Solved Threads: 0
javafrustrated javafrustrated is offline Offline
Newbie Poster

Palindromes

 
0
  #1
Dec 17th, 2008
The program is intended to read a file with a few lines of strings to determine whether or not they are palindromes. I'm getting an error while compiling - tagged where the error shows up. I'm not sure whether or not it is going to read the lines correctly from the file or not either since I can't compile it.

[code]
import java.io.*;

public class Palindromes
{
public static BufferedReader inFile;
public static void main (String[]args)throws IOException
{


inFile = new BufferedReader(new FileReader("palindrome.dat"));

System.out.println("Enter a word or phrase: ");
String s = inFile.readLine();
s = s.toLowerCase();
String toTest = s;
int index;

for(int index = 0;index < s.length();index++)
{
if(Character.isLetterOrDigit(s.charAt(index))
cleanStr += s.charAt(index); //error shows up here - it is looking for a "{" ?
}

if(isPalindrome(toTest))
System.out.println(s + " IS A PALINDROME");

else
System.out.println(s + " IS NOT A PALINDROME");

}


public static boolean isPalindrome(String s)
{
if (s.length() <= 1)
return true; // Base case
else
{
if (s.charAt(0) == s.charAt(s.length() - 1))
return isPalindrome(s.substring(1, s.length() - 1 ) );
else
return false;
}
}
[code]
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Palindromes

 
0
  #2
Dec 17th, 2008
No, actually it's expecting a ")". Count them on that line.

(Also, you need to use [/code] as the end tag after your code. Note the slash.)
Last edited by Ezzaral; Dec 17th, 2008 at 3:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 11
Reputation: javafrustrated is an unknown quantity at this point 
Solved Threads: 0
javafrustrated javafrustrated is offline Offline
Newbie Poster

Re: Palindromes

 
0
  #3
Dec 17th, 2008
I missed that silly bracket. Also with the [code] at the end I realized my stupidity as soon as I pressed post. I'm just really tired of working on this program.

I now get an error that it can't find cleanStr - does this have to be set up in the variables?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Palindromes

 
0
  #4
Dec 17th, 2008
If you want to use it, it does. Any variable must be explicitly declared.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: Palindromes

 
0
  #5
Dec 17th, 2008
Hi there, I'm not entirely sure why you're running that for loop that contains the undeclared variable. Remember that the end of line terminator is not returned by the readLine() method of BufferedReader. Also, if you just want to get rid of leading/following whitespaces (like a space) then you can use the trim() method of the String class: s = s.trim()
You don't even use the cleanStr as the parameter of your static method.

One comment about the recursion you've used, in a programming language such as Java, recursion is not always a better solution than a for loop. But I'm guessing the question required the use of recursion? Anyway, don't get too frustrated, Java is rather beautiful
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 11
Reputation: javafrustrated is an unknown quantity at this point 
Solved Threads: 0
javafrustrated javafrustrated is offline Offline
Newbie Poster

Re: Palindromes

 
0
  #6
Dec 17th, 2008
This is my program now -- and yes unfornately I had to use recursion. I got the program working except it is only reading the first line of the file and I'm not sure where to put another readLine for it to read the rest of the file. My revised program
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class PalindromesHelp
  5. {
  6. public static BufferedReader inFile;
  7. public static void main (String[]args)throws IOException
  8. {
  9.  
  10.  
  11. inFile = new BufferedReader(new FileReader("palindrome.dat"));
  12.  
  13.  
  14. String s = inFile.readLine();
  15. s = s.toLowerCase();
  16. String toTest = "";
  17.  
  18.  
  19. for(int index = 0;index < s.length();index++)
  20. {
  21. if(Character.isLetterOrDigit(s.charAt(index)))
  22. toTest= toTest.toLowerCase();
  23.  
  24. }
  25.  
  26. if(isPalindrome(toTest))
  27. System.out.println(s + " IS A PALINDROME");
  28.  
  29. else
  30. System.out.println(s + " IS NOT A PALINDROME");
  31.  
  32. }
  33.  
  34.  
  35. public static boolean isPalindrome(String s)
  36. {
  37. if (s.length() <= 1)
  38. return true; // Base case
  39. else
  40. {
  41. if (s.charAt(0) == s.charAt(s.length() - 1))
  42. return isPalindrome(s.substring(1, s.length() - 1 ) );
  43. else
  44. return false;
  45.  
  46. }
  47. }
  48. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: Palindromes

 
0
  #7
Dec 17th, 2008
Oh yes I was actually going to comment on that. As it stands your program would only cater for the first line because you're only asking it to read the first line. In order to read the entire file what structure do you think would be required?

On that note, a reminder that the readLine() method returns null once you're reached the end of the file. Well that's actually more of a hint than a reminder hehe

Oh and something that you might want to look into is the Scanner class: it makes reading data from a file so much easier!
Last edited by PoovenM; Dec 17th, 2008 at 4:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 11
Reputation: javafrustrated is an unknown quantity at this point 
Solved Threads: 0
javafrustrated javafrustrated is offline Offline
Newbie Poster

Re: Palindromes

 
0
  #8
Dec 17th, 2008
I don't really know what it is I'm supposed to do anymore -- all I want to do is read the file.

We haven't learned how to use the Scanner for input and I can't bear to learn anymore
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: Palindromes

 
1
  #9
Dec 17th, 2008
Aww I can see that you need sleep hehe you'd need a looping structure to read the entire file! I guess I could have given you a better hint!

The basic idea is that you'd read from the file while the input is not null. What you'd do is use a loop that looked something like this:
  1. String s = inFile.readLine(); //the loop should start after this line
  2. while (s !=null)
  3. {
  4. ...
  5. s = inFile.readLine(); //here you change the value of s for checking
  6. }
Where ... refers to the code after the line String s = inFile.readLine();

If you wanted to be a bit more fancy you could have just had:
  1. while(s=readLine() != null)
  2. {
  3. ...//notice that s is not updated in the body of the loop
  4. }

If you need help understanding this, please let me know. The basic idea of a loop is to initialize, check the test variable/condition and finally to change the test variable/condition within the loop body. Using this, you'd never go wrong!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 11
Reputation: javafrustrated is an unknown quantity at this point 
Solved Threads: 0
javafrustrated javafrustrated is offline Offline
Newbie Poster

Re: Palindromes

 
0
  #10
Dec 18th, 2008
I am actually getting a compiling error now at the start of my boolean saying it is an illegal start of an expression?????


  1. inFile = new BufferedReader(new FileReader("palindrome.dat"));
  2. String s = inFile.readLine();
  3.  
  4. while( s !=null)
  5. {
  6. s=inFile.readLine();
  7. }
  8.  
  9. String lowCase = s.toLowerCase();
  10. String toTest = "";
  11. int index;
  12.  
  13.  
  14. for(int index = 0;index < s.length();index++)
  15. {
  16. if(Character.isLetterOrDigit(s.charAt(index)))
  17. toTest= toTest.toLowerCase();
  18.  
  19. }
  20.  
  21. if(isPalindrome(toTest))
  22. System.out.println(s + " IS A PALINDROME");
  23.  
  24. else
  25. System.out.println(s + " IS NOT A PALINDROME");
  26.  
  27. }
  28.  
  29.  
  30. public static boolean isPalindrome(s)
  31. {
  32. if (s.length() <= 1)
  33. return true; // Base case
  34. else
  35. {
  36. if (s.charAt(0) == s.charAt(s.length() - 1))
  37. return isPalindrome(s.substring(1, s.length() - 1 ) );
  38. else
  39. return false;
  40.  
  41. }
  42.  
  43. }
  44. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC