943,786 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1799
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 17th, 2008
0

Palindromes

Expand Post »
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]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javafrustrated is offline Offline
11 posts
since Dec 2008
Dec 17th, 2008
0

Re: Palindromes

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Dec 17th, 2008
0

Re: Palindromes

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javafrustrated is offline Offline
11 posts
since Dec 2008
Dec 17th, 2008
0

Re: Palindromes

If you want to use it, it does. Any variable must be explicitly declared.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Dec 17th, 2008
0

Re: Palindromes

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
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006
Dec 17th, 2008
0

Re: Palindromes

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
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javafrustrated is offline Offline
11 posts
since Dec 2008
Dec 17th, 2008
0

Re: Palindromes

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.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006
Dec 17th, 2008
0

Re: Palindromes

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javafrustrated is offline Offline
11 posts
since Dec 2008
Dec 17th, 2008
1

Re: Palindromes

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:
java Syntax (Toggle Plain Text)
  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:
java Syntax (Toggle Plain Text)
  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!
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006
Dec 18th, 2008
0

Re: Palindromes

I am actually getting a compiling error now at the start of my boolean saying it is an illegal start of an expression?????


Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javafrustrated is offline Offline
11 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Beginner Need Help!
Next Thread in Java Forum Timeline: Problem with Arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC