Results 1 to 4 of 4
  1. #1
    vuzuggu is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Counting number of palindromes in a file

    I am trying to develop a java program that will count the number of palindromes in a file. so far I have got access to the file but am not sure how to proceed.

    Java Code:
    import java.io.*;
    public class palindromes {	  
    	public static void main(String[] args) throws Exception {
    	
    		File file = new File("C:\\test.txt"); //select file
    	    FileReader fr = new FileReader(file);
    	    BufferedReader br = new BufferedReader(fr);
    	    StringBuffer sb = new StringBuffer();
    	    String eachLine = br.readLine();
    	    int numberpalindromes = 0;
    
    	    while (eachLine != null) {
    	      sb.append(eachLine);
    	      sb.append(" "); //puts space between lines
    	      eachLine = br.readLine();
    	      
    	    }
    	    
    	    System.out.println(sb.toString().length()); //testing java can access file.
    	  
    	  }
    
    }
    Any tips would be really appreciated!

  2. #2
    SFNYC is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default Re: Counting number of palindromes in a file

    StringBuilder has a reverse method. If a word equals the reverse of itself, then you have a palindrome.

  3. #3
    vuzuggu is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: Counting number of palindromes in a file

    Thanks for that! Im very new to java my only other program was a hello world one! Would you be able to give a few more hints and layout of the code. I donlt want it done for me as I want to learn but i have hit a brick wall atm!

  4. #4
    SFNYC is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Counting number of entries in a file
    By lenny0 in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 03:40 AM
  2. Counting total number of images in a website
    By sharifiit in forum Networking
    Replies: 0
    Last Post: 04-30-2011, 08:20 AM
  3. Finding out number of Palindromes in a Sentence
    By rameshiit19 in forum New To Java
    Replies: 1
    Last Post: 09-13-2010, 03:44 AM
  4. counting number of lines of system.out
    By IYIaster in forum New To Java
    Replies: 1
    Last Post: 07-21-2009, 12:37 AM
  5. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 05:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •