Results 1 to 8 of 8
  1. #1
    dylanka is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Can someone help with me code?

    Hi, all. So new to programming, just doing it as a hobby. But at the moment, I am trying to make one piece of code. So, what I want it do do, is, open up a text file, then check, identify, and count the number of palindromes (Words the same backwards e.g. did, abba, otto). But I am doing something wrong with that code.

    I have a text file. It has 10 palindromes in it. Here is what it is:

    zerimar ramirez reviver of lawns from wassamassaw south carolina used a rotavator to cut through the marram grass invading gardens near the beach
    otto sees otto said otto as he passed a mirror He did eh responded his mother avid diva his cousin exclaimed repel a leper on seeing his hand I can cure that said his grandmother from navan meath don't be silly they're not straw warts they won't rot from your witch-doctoring replied otto's mother
    So just a load of random words pretty much. But the palindromes are:

    reviver
    wassamassaw
    a
    rotavator
    marram
    otto
    sees
    did
    navan
    i

    But, in my code, when I run it, it is very wrong.

    Firstly, it picks words together, and says they are a ppalindrome, example is avid diva. It is appearing that this is 2 palindromes. When it isnt even one.

    It is also, something I'll need to add or something, but its bringing palindromes that I have more than one of, and counting for each one. I want it, so, for example, Otto, is only counted once, and not three times.

    And also, when the window comes up, the result, does it twice, with different results thrice. Im not explaining this part well, but even if the other 2 were to be corrected, I'd be extremely happy.

    Here is the code I have now

    Java Code:
    import java.io.*;
    
    
    public class PC {
    	public static void main(String[] args) {
    		
    		try { 
    				  
    			FileInputStream fstream = new FileInputStream("C:/Test1.txt"); 
    				  
    			DataInputStream in = new DataInputStream(fstream); 
    			BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    			String strLine = null;
    			
    			while ((strLine = br.readLine()) != null)   {
    	
    				  
    				
    					String reverse = new
    					
    					StringBuffer(strLine).reverse().
    					toString();
    					int i,j,counter=0;
    					
    					String m[]=strLine.split(" ");
    					String[] word=reverse.split(" ");
    					
    					
    					System.out.println("The palindrome words are:");
    					for(i=0;i<m.length;i++) {
    						for(j=word.length-1;j>=0;j--) {
    							if(m[i].equalsIgnoreCase(word[j])) {
    								System.out.println(m[i]);
    								counter++;
    								break;
    							}
    							
    						}
    					}
    					System.out.println("Number of palindromes:"+counter);
    			}
    		}
    				
    		catch(IOException e){}
    				
    		
    	
    	}
    }
    Any help will be appreciated. Thanks :D

  2. #2
    dheaven is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Can someone help with me code?

    You could try using
    Java Code:
    //For each string in string array
    for(String str: m)
    {
     //Check if it is palindrome
     if(str == str.reverse())
      counter++
    }
    instead of two for.

  3. #3
    dylanka is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Can someone help with me code?

    Hi, I did this, but there is a different problem now. It brings up the error:

    The method reverse() is undefined for the type String

    I am very new to this, so there is probably a simple explanation to this. Maybe I am going to fast...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Can someone help with me code?

    Quote Originally Posted by dheaven View Post
    You could try using
    Java Code:
    //For each string in string array
    for(String str: m)
    {
     //Check if it is palindrome
     if(str == str.reverse())
      counter++
    }
    instead of two for.
    Why in the name of all things holy would you recommend that he use == to compare Strings? He was doing the String comparison right, and now you recommend he do it wrong??

  5. #5
    dylanka is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Can someone help with me code?

    Anyone else have any more advice. Looks so many times over it, but I dunno what to do, but dont want to leave it :p

  6. #6
    Kami is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Can someone help with me code?

    Quote Originally Posted by dheaven View Post
    You could try using
    Java Code:
    //For each string in string array
    for(String str: m)
    {
     //Check if it is palindrome
     if(str == str.reverse())
      counter++
    }
    instead of two for.
    It was my understanding that you should use .equals() when comparing strings. Is this wrong?
    Java Code:
    for(String str: m) {
     if(str.equals(str.reverse()))
      counter++
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Can someone help with me code?

    Quote Originally Posted by Kami View Post
    It was my understanding that you should use .equals() when comparing strings. Is this wrong?
    Java Code:
    for(String str: m) {
     if(str.equals(str.reverse()))
      counter++
    }
    You are right, your "helper" is most definitely wrong.

  8. #8
    dheaven is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Can someone help with me code?

    Sorry about that, I wrote it in a hurry.
    Still, I don't think the sarcasm was necessary (especially when you're a moderator).
    The code should look like this:
    Java Code:
    for(String str: m) {
    			 if(str.equals(new StringBuilder(str).reverse().toString()))
    			  counter++;
    			}

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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