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.

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);  [COLOR="Red"]//error shows up here - it is looking for a "{" ?[/COLOR]  
    }   

         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;
         }
      }

Recommended Answers

All 12 Replies

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.)

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?

If you want to use it, it does. Any variable must be explicitly declared.

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 ;)

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

import java.io.*;
import java.util.*;

    public class PalindromesHelp
   {
	public static BufferedReader inFile;
       public static void main (String[]args)throws IOException
      {
         
            
         inFile = new BufferedReader(new FileReader("palindrome.dat"));
   
         
         String s = inFile.readLine();
			s = s.toLowerCase();
         String toTest = "";
			

  for(int index = 0;index < s.length();index++)
{
    if(Character.isLetterOrDigit(s.charAt(index)))
    	  toTest= toTest.toLowerCase();
  
	}	
		
         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;
					
         }
      }
 }

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!

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

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:

String s = inFile.readLine(); //the loop should start after this line
while (s !=null)
{
...
s = inFile.readLine(); //here you change the value of s for checking
}

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:

while(s=readLine() != null)
{
...//notice that s is not updated in the body of the loop
}

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!

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

inFile = new BufferedReader(new FileReader("palindrome.dat"));
         String s = inFile.readLine();
         
         while( s !=null)
{
s=inFile.readLine();
}

     String lowCase = s.toLowerCase();
     String toTest = "";
     int index;

  
    for(int index = 0;index < s.length();index++)
{
    if(Character.isLetterOrDigit(s.charAt(index)))
    	  toTest= toTest.toLowerCase();
  
	}	
		
         if(isPalindrome(toTest))
            System.out.println(s + " IS A PALINDROME");
         
         else
            System.out.println(s + " IS NOT A PALINDROME");
      
      }
   
   
      public static boolean isPalindrome(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;
				
         }
			
      }
 }

When I said, 'Where ... refers to the code after the line String s = inFile.readLine();' I meant all of the code. A loop will allow for a set of instructions to be executed 0, 1 or more times. If all you want to repeat is reading the data from the file, then that leaves you with only processing the last line of the file.

You need to read in the data and process it and that's what needs to be repeated and thus placed in a loop.

Anyway, what do you mean by, 'at the start of my boolean?' Please use the so line numbers appear. That way you can tell us which line the error occurred. The expression in the while loop is not incorrect and would compile.

Regardless, by looking at your code I can see that you're missing some braces. Particularly, you don't close the [B]for[/B] loop nor do you close the [B]main[/B] method. This is why you're getting the error.[code=java] so line numbers appear. That way you can tell us which line the error occurred. The expression in the while loop is not incorrect and would compile.

Regardless, by looking at your code I can see that you're missing some braces. Particularly, you don't close the for loop nor do you close the main method. This is why you're getting the error.

First of all:
Next time post what kind of error you get and where you get it so we wouldn't have to search the entire code for the error.

And second:

This:

public static boolean isPalindrome(s) {

}

should be:

public static boolean isPalindrome(String s) {

}

And you don't have to use recursion to test whether something is a palindrome or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.